另一个简单的问题是更改 UIButton 的背景颜色。所以我在搜索了一下之后发现,一种常见的方法是创建图像或 subview 。
我在 Storyboard中创建了一个按钮并将其连接到 socket 。在我的 VC 课上,我有以下内容
.h
@property (nonatomic, readwrite, strong) IBOutlet UIButton *uiLoginButton;
.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"%s and frame rect %@", __PRETTY_FUNCTION__, self.uiLoginButton); //here uiloginbutton does not have any size!
CGRect buttonRect = CGRectMake(50, 50, 100.0, 100.0);
self.uiLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.uiLoginButton.frame = buttonRect;
self.uiLoginButton.backgroundColor = [UIColor redColor];
NSLog(@"%s and frame rect %@", __PRETTY_FUNCTION__, self.uiLoginButton);
}
不,我随机设置了 CGRectMake 中的值,因为 IBOutlet 按钮的框架为 0。怎么会这样?
实际更改按钮颜色的正确方法是什么?
最佳答案
如果您在 nib 中创建一个按钮,您可以通过 nib 本身更改所有属性。 像这样
或
如果你想通过代码来做,因为你是通过 nib 点来添加它的,需要注意
- 不需要初始化。因为它是 IB 的属性。
- 只需通过 IB 直接提供框架或通过
-setFrame:
方法在代码中提供框架 - 要设置颜色,可以使用
-setBackgroundcolor:
方法
所以我的.h
@property (nonatomic,strong) IBOutlet UIButton *uiLoginButton;
.m
[self.uiLoginButton setFrame:buttonRect];
[self.uiLoginButton setBackgroundColor:[UIColor redColor]];
关于ios - 另一个 UIButton 背景颜色问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15455082/