动态按钮创建不起作用

时间:2015-07-23 14:43:06

标签: ios objective-c iphone uibutton dynamically-generated

无法弄清楚出了什么问题 - 这是UIViewController中 viewDidLoad()的实现; 我动态创建了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);}

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

UIImageView和UILabel效果很好。

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

2 个答案:

答案 0 :(得分:0)

我认为问题在于代码..

self.buttonTraining.frame.size.height有时最初为null。你在动态创建按钮后最初设置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
}

一旦检查.. 希望它可以帮助你......!

答案 1 :(得分:0)

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