动态按钮创建不起作用

Dynamic Button creation doesn't work

无法弄清楚出了什么问题 - 这是 viewDidLoad() 在 UIViewController 中的实现; 我动态创建了 3 个不同的 UI 元素:UIImageView、UIButton 和 UILabel; 虽然它们的启动方式几乎相同,但事实证明按钮创建不正确!

    - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.


self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height, self.view.frame.size.width, self.view.frame.size.width)];
[self.view addSubview:self.mainImageView];
self.mainImageView.image = [UIImage imageNamed:@"main"];


self.buttonTraining = [UIButton buttonWithType:UIButtonTypeSystem];
[self.view insertSubview:self.buttonTraining aboveSubview:self.mainImageView];
self.buttonTraining.backgroundColor = [UIColor redColor];
self.buttonTraining.tintColor = [UIColor whiteColor];
self.buttonTraining.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:30];
[self.buttonTraining setTitle:NSLocalizedString(@"Training", nil) forState:UIControlStateNormal];
[self.buttonTraining sizeToFit];

if (self.view.frame.size.height <= 480) {
    self.buttonTraining.frame = CGRectMake(0, self.mainImageView.frame.origin.y + self.mainImageView.frame.size.height, self.view.frame.size.width, self.buttonTraining.frame.size.height);
} else {
    self.buttonTraining.frame = CGRectMake(0, self.mainImageView.frame.origin.y + self.mainImageView.frame.size.height, self.view.frame.size.width, self.buttonTraining.frame.size.height * 2);
}

[self.buttonTraining addTarget:self action:@selector(startTraining) forControlEvents:UIControlEventTouchUpInside];


self.titleLabel = [UILabel new];
[self.view insertSubview:self.titleLabel aboveSubview:self.mainImageView];
self.titleLabel.textColor = [UIColor whiteColor];
self.titleLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:30];
self.titleLabel.textAlignment = NSTextAlignmentCenter;
self.titleLabel.text = @"text";
[self.titleLabel sizeToFit];

self.titleLabel.frame = CGRectMake((self.view.frame.size.width - self.titleLabel.frame.size.width) / 2.0f + 18, self.navigationController.navigationBar.frame.origin.y + self.navigationController.navigationBar.frame.size.height + 8, self.titleLabel.frame.size.width, self.titleLabel.frame.size.height);}

问题是我无法重现问题!我在 iPhone 模拟器 (iPhone 4s - iPhone 6 Plus) 和我的 iPhone 5 上测试了应用程序(均通过连接到 Xcode 并通过使用临时配置进行分发),iOS 8.0-8.4。一切都好。但是我收到了没有按钮的屏幕截图(App Store 评论),他们说启动应用程序后它就没有出现。

UIImageView 和 UILabel 效果很好。

你有什么想法吗?!我可以保证它是我的代码中唯一影响这些 UI 元素的地方。提前致谢!

我认为问题出在下面的代码中..

self.buttonTraining.frame.size.height 有时最初为空。动态创建按钮后,您最初在哪里设置 buttonTraining 高度或框架?

if (self.view.frame.size.height <= 480) {
    self.buttonTraining.frame = CGRectMake(0, self.mainImageView.frame.origin.y + self.mainImageView.frame.size.height, self.view.frame.size.width, self.buttonTraining.frame.size.height);
} else {
    self.buttonTraining.frame = CGRectMake(0, self.mainImageView.frame.origin.y + self.mainImageView.frame.size.height, self.view.frame.size.width, self.buttonTraining.frame.size.height * 2);
}

所以,而不是 self.buttonTraining.frame.size.height 使用一些静态值或如下所示

if (self.view.frame.size.height <= 480) {
    self.buttonTraining.frame = CGRectMake(0, self.mainImageView.frame.origin.y + self.mainImageView.frame.size.height, self.view.frame.size.width, self.mainImageView.frame.size.height);//set static value for self.buttonTraining.frame.size.height
} else {
    self.buttonTraining.frame = CGRectMake(0, self.mainImageView.frame.origin.y + self.mainImageView.frame.size.height, self.view.frame.size.width, self.mainImageView.frame.size.height * 2);//set static value for self.buttonTraining.frame.size.height
}

检查一次.. 希望对你有帮助...!

问题已被@TomSwift 解决,他指出动态创建的按钮应该是强属性,而不是弱