Mac开发之如何设置NSButton高亮图片

阅读: 评论:0

Mac开发之如何设置NSButton高亮图片

Mac开发之如何设置NSButton高亮图片

Mac开发之如何设置NSButton高亮图片


iOS开发设置UIButton高亮图片非常简单,但Mac开发和iOS开发略有不同,在NSButton的setImage并没有设置 UIControlState的参数,因此要设置NSButton的高亮图片是要有一定波折的。这里讲两种方式设置button高亮图片:纯代码方式和IB方式。

一、纯代码:
//
//  ViewController.m
//  ButtonSetHighlightTest
//
//  Created by 凌       陈 on 9/5/17.
//  Copyright © 2017 凌       陈. All rights reserved.
//#import "ViewController.h"@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view.// 实例化一个NSButtonNSButton *button = [[NSButton alloc] initWithFrame:CGRectMake(20, 20, 60, 30)];// 设置button normal状态图片[button setImage:[NSImage imageNamed:@"apress"]];// 设置button highlight状态图片[(NSButtonCell *)ll setHighlightsBy:NSContentsCellMask];button.alternateImage = [NSImage imageNamed:@"apress1"];// 设置图片铺满ll setImageScaling:NSImageScaleAxesIndependently];// 去掉button边框[button setBordered:NO];[button bezelStyle];// 设置响应事件button.target = self;button.action = @selector(buttonAction);// 将button加到view中[self.view addSubview:button];}- (void)setRepresentedObject:(id)representedObject {[super setRepresentedObject:representedObject];// Update the view, if already loaded.
}// button响应事件
-(void) buttonAction {NSLog(@"你按下了按键1!");
}@end

二、IB 打开Main.storyboard,拖一个button到ViewController,将button按照如下图设置


UIControlState

UIControlState

UIControlState

本文发布于:2024-01-29 17:29:16,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170652055817073.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:如何设置   图片   Mac   高亮   NSButton
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23