2013-02-04 35 views
4

我想创建一个具有渐变背景的UIButton。我得到的工作很好,但按钮不会突出显示(默认行为是为了让按钮变暗)。如何使用渐变创建UIButton并突出显示?

这里是我的按钮:

-(UIButton *)createLoginButtonForSize:(CGSize)size { 
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    loginButton.translatesAutoresizingMaskIntoConstraints = FALSE; 
    loginButton.layer.cornerRadius = 8; 
    loginButton.titleLabel.text = @"Login"; 

    [loginButton addTarget:self action:@selector(loginCheck:) forControlEvents:UIControlEventTouchUpInside]; 


    [loginButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:[loginButton(WIDTH)]" 
                     options:0 
                     metrics:@{@"WIDTH": [NSNumber numberWithFloat:size.width]} 
                      views:NSDictionaryOfVariableBindings(loginButton)]]; 

    [loginButton addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[loginButton(HEIGHT)]" 
                     options:0 
                     metrics:@{@"HEIGHT": [NSNumber numberWithFloat:size.height]} 
                      views:NSDictionaryOfVariableBindings(loginButton)]]; 

    CAGradientLayer *layer = [UIColor greenGradient]; 
    layer.frame = CGRectMake(0, 0, size.width, size.height); 
    layer.cornerRadius = 8; 

    [loginButton.layer insertSublayer:layer atIndex:0]; 

    return loginButton; 
} 

我需要处理突出自己?

回答

5

是的,你需要自己处理突出显示。虽然不是自己编写代码,但应该检查Jeff Lamarche的易用性iPhone Gradient Buttons Project。它完全正是你想要做的。这只是2个文件,所以很容易地融入到您的项目:下面

http://code.google.com/p/iphonegradientbuttons/source/browse/trunk/Classes/GradientButton.h http://code.google.com/p/iphonegradientbuttons/source/browse/trunk/Classes/GradientButton.m

截图来自Jeff's Blog discussing the project拍摄。

Imageless gradient buttons

+0

完美,节省了我的时间从做繁重的工作,谢谢! – Padin215

+0

没问题。干杯! :) – memmons