2017-05-10 2 views
1

단추 스타일 설정에 문제가 있습니다. Apple 문서를 이미 읽었지만 스타일을 올바르게 변경하는 방법을 여전히 모릅니다. "NSButton 사용자 지정 스타일 프로그래밍 방식으로 <OSX/Objective-C>

[loginBtn.layer setBackgroundColor:[NSColor colorWithCalibratedRed:35/255.0f green:216/255.0f blue:202/255.0f alpha:1.0f]CGColor]; 

내가 제대로 목표 - C에서와의 backgroundColor를 설정하는 방법 : 여기

내 코드입니다 :

NSButton *loginBtn = [[NSButton alloc] initWithFrame:NSMakeRect(screenWidth/2 - 155, screenHeight/2 - 115, 300, 30)]; 
[loginBtn setCell:defaultButton]; 
[loginBtn setWantsLayer:YES]; 
[loginBtn setButtonType:NSButtonTypeMomentaryPushIn]; 
NSMutableAttributedString *buttonString = [[NSMutableAttributedString alloc] initWithString:@"Login" attributes:btnTextDictionary]; 
[loginBtn setAttributedTitle:buttonString]; 
[loginBtn setTarget:self]; 
[loginBtn setAction:@selector(loginActive)]; 
[loginView addSubview:loginBtn]; 

나는 RGB로 배경색을 변경하려고하지만, 작동하지 않는 OSX 응용 프로그램 프로젝트 "?

+0

NSButton이없는 'backgroundColor'속성. – norders

답변

0

테두리 배경을 설정하여 테두리가있는 NSButton의 색상을 변경할 수 없습니다. 귀하의 경우에는 [loginBtn setBordered: NO];으로 설정해야합니다.

https://github.com/OskarGroth/FlatButton

FlatButton for macOS

: 당신이 당신의 솔루션에 대한 더 빠르고 다양한 옵션을 원하는 경우

, 당신이 바로 인터페이스 빌더에서 쉽게 버튼 스타일 만들하자 FlatButton라는 편리한 NSButton 서브 클래스를했습니다

신속한 답변입니다.

+0

정말 고마워요! 방금 설정했습니다. "아니오"로 설정하면 작동합니다! –

0

Objective-C의 경우 여기 setBackgroundColor 메서드를 제공하는 NSButton의 하위 클래스가 있습니다. 메인 코드에서

MyButton.h

#import <Cocoa/Cocoa.h> 

@interface MyButton : NSButton 
@property (nonatomic, strong) NSColor *backgroundColor; 
@end 

MyButton.m

#import "MyButton.h" 

@implementation MyButton 

- (void)setBackgroundColor:(NSColor *)backgroundColor { 
    _backgroundColor = backgroundColor; 
    [self setNeedsDisplay]; 
} 

- (void)drawRect:(NSRect)dirtyRect { 
    [super drawRect:dirtyRect];  
    [_backgroundColor setFill]; 
    NSRectFill(dirtyRect); 
} 

@end 

당신은이 작업을 수행 할 수 있습니다

#import "MyButton.h" 

MyButton *loginBtn = [[MyButton alloc] initWithFrame:NSMakeRect(screenWidth/2 - 155, screenHeight/2 - 115, 300, 30)]; 
[loginBtn setBackgroundColor:[NSColor colorWithCalibratedRed:35/255.0f green:216/255.0f blue:202/255.0f alpha:1.0f]]; 
관련 문제