ios – 以编程方式编码的UIButton未在我的视图中显示

前端之家收集整理的这篇文章主要介绍了ios – 以编程方式编码的UIButton未在我的视图中显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
.

你好,

我正在尝试以编程方式将UIButton和其他项添加到我的UIViewController中,并且从我在其他SO问题中阅读和看到的内容我应该使用以下代码在正确的轨道上:

- (void)viewDidLoad
{
[super viewDidLoad];



UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];    
backButton = [[UIButton alloc] init];
backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(10,25,150,75);
backButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
backButton.titleLabel.text = @"Back"; 
[self.view addSubview:backButton];


// Do any additional setup after loading the view.
}

现在代码放在viewDidLoad方法中并且它没有出现,这引出了我的问题 – 我做错了什么?

当我想要更改背景的颜色并放入viewDidLoad时它可以正常工作.

干杯杰夫

解决方法

我认为你可能正确地插入按钮,但是因为它没有背景并且文本没有显示而无法看到它.

尝试使用圆角矩形按钮系统按钮,只是为了看它是否显示.正确设置文本.我也将删除设置字体,只是因为字体有问题.

// backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
backButton = [UIButton buttonWithType:UIButtonTypeSystem];
backButton.frame = CGRectMake(10,75);
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[self.view addSubview:backButton];

更新:UIButtonTypeRoundedRect已被弃用.

原文链接:https://www.f2er.com/iOS/333955.html

猜你在找的iOS相关文章