创建了一个带有渐变图层的按钮:
//MyGradient creates and returns gradient layers
CAGradientLayer *gradient = [MyGradient blueGradient];
//MyButton is a subclassed UIButton which adds some attributes
MyButton *testButton = [MyButton buttonWithType:UIButtonTypeCustom];
testButton.frame = CGRectMake(10, 10, 150, 50);
[testButton addTarget:self action:@selector(testButton:) forControlEvents:UIControlEventTouchUpInside];
[gradient setFrame:testButton.bounds];
testButton.clipsToBounds = YES;
[testButton.layer insertSublayer:gradient atIndex:0];
[self.view addSubview:testButton];
我怎样才能让图像在按下时突出显示?我找到了几个示例代码,它们将 UIButton 子类化并覆盖 drawRect: 方法。有没有更简单的方法让按钮高亮显示?
最佳答案
您的按钮没有图像 - 所以没有什么可突出显示的。
设置一个图像(一个渐变图像,如果你想要的话)作为按钮的背景......即使你没有为 UIControlStateHighlighted 设置图像,按钮也会“突出显示” - 基本上,iOS 会反转颜色。
另外,如果你只需要水平/垂直渐变,你可能想要检查这个函数:resizableImageWithCapInsets
玩得开心!
关于objective-c - 带有不突出显示的渐变层的 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10688444/