对于UIButton我正在使用第一个UIButtonTypeCustom现在我想将其更改为RoundedRect但是当我将UIButtonTypeCustom更改为UIButtonTypeRoundedRect时,它会显示白色的RoundedRect而不是为其指定的颜色,并且它显示在roundrect按钮后面的rect buttonframe的边缘。所以我想知道如何解决这个问题。
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(0, 0, 60, 30);
[myButton setTitle:@"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor colorWithHue:1.0/12 saturation:2.0/3 brightness:4.0/10 alpha:1.0];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setLeftBarButtonItem:button animated:YES];
感谢您的帮助。
答案 0 :(得分:9)
嗯......我担心如果它是UIButton
,就无法更改UIButtonTypeRoundedRect
的backgroundColor。
我的建议是创建UIButtonTypeCustom
并更改其图层以模仿UIButtonTypeRoundedRect
。
试试这段代码:
#import <QuartzCore/QuartzCore.h>
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
[myButton.layer setBackgroundColor:[UIColor whiteColor].CGColor;