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已被弃用.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...