我正在尝试创建一个具有渐变背景的 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;
}
我需要自己处理突出显示吗?
最佳答案
是的,您需要自己处理突出显示。不过,与其滚动自己的代码,不如查看 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 .
关于ios - 如何创建带有渐变和高亮的 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14691372/