ios - 另一个 UIButton 背景颜色问题

标签 ios uibutton

另一个简单的问题是更改 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 本身更改所有属性。 像这样 enter image description here

如果你想通过代码来做,因为你是通过 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/

相关文章:

iphone - 使用 UIButton 更改 UIScrollView?

iphone - 将返回的数据传递到数组,iOS

ios - xibs 3.5inch 和 4inch 的命名约定

ios - 从 IBDesignable 中的自定义控件的框架加载代码

ios - 检测在 tableview : Swift Best Practices 中按下的 uibutton

iphone - UIButton 图像在 IB 中看起来不错,但在应用程序中显示时会被拉伸(stretch)

ios - Swift:删除 UIButton attributedTitle

ios - 基于iOS定位的图层蒙版

ios - 从 TabBarController 的 subview 在 TabBar 后面插入 subview

ios - 具有 anchor 样式自动布局约束的 UIButtons 未显示