2013-02-04 46 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