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.
// 实例化一个NSButton
NSButton *button = [[NSButton alloc] initWithFrame:CGRectMake(20, 20, 60, 30)];
// 设置button normal状态图片
[button setImage:[NSImage imageNamed:@"apress"]];
// 设置button highlight状态图片
[(NSButtonCell *)button.cell setHighlightsBy:NSContentsCellMask];
button.alternateImage = [NSImage imageNamed:@"apress1"];
// 设置图片铺满button
[button.cell 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