【发布时间】:2016-04-22 10:04:54
【问题描述】:
我在 Objective C 上用 xcode 开发了一个项目。前两天一切都很好。现在我所有的按钮和其他控件都没有显示它们的标题文本或图像。我尝试从情节提要中添加新按钮,但它的文本仍未显示。 我也尝试从代码中添加新按钮,但结果相同。
- (void) addTestButtonProgramatically {
testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[testButton addTarget:self
action:@selector(testButtonSelector:)
forControlEvents:UIControlEventTouchUpInside];
[testButton setTitle:@"TEST TEXT" forState:UIControlStateNormal];
[testButton setBackgroundColor:[UIColor colorWithRed:0.6 green:0.1 blue:1.0 alpha:1.0]];
testButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:testButton];
}
- (IBAction)testButtonSelector:(id)sender {
[testButton.titleLabel sizeToFit]; //this row make text of my button shown
}
也许我错误地更改了导致这种行为的设置。我尝试在另一台计算机上午餐我的项目,但问题仍然存在。
然后我尝试创建新的清晰项目,一切都很好,按钮文本可见。
编辑:
我找到了问题的原因。这是因为我在 UIButton 类别上重新定义了 - (void) layoutSubvies 方法。
@interface UIButton (Coordinator)
@end
@implementation UIButton (Coordinator)
- (void) layoutSubviews {
[super layoutSubviews];
}
@end
然后感谢所有帮助我找到它的人。
【问题讨论】:
-
你在哪里调用了这个方法addTestButtonProgramatically
-
我在 [super viewWillAppear:animated] 之前从 - (void)viewWillAppear:(BOOL)animated 调用此方法;
-
而不是 viewWillAppear 添加 -(void)viewDidLayoutSubviews。我希望这会有所帮助
-
问题仍然存在。我重复本质,我所有的按钮和导航栏项目都显示没有文本和图像,只有当我让 [button.titleLabel sizeToFit] 它显示文本值。
-
从 viewDidload 调用它..
标签: ios objective-c xcode uibutton