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.
    
    // 实例化一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值