iOS:UIButton 不显示

标签 ios objective-c cocoa-touch uiview uibutton

我正在创建一个简单的按钮并将其作为 subview 添加到主视图。虽然它没有出现。

这是设置它的代码:

UIButton* contactButton = [[UIButton alloc] init];
contactButton.titleLabel.text = @"Contact Developer";
[contactButton sizeToFit];
[self.view addSubview:contactButton];
NSLog(@"X: %f || Y: %f || Width: %f || Height: %f", contactButton.frame.origin.x, contactButton.frame.origin.y, contactButton.frame.size.width, contactButton.frame.size.height);

您可能已经注意到,我在最后放置了一些调试代码以查看按钮的位置和尺寸。它们是:X:0,Y:0,宽度:30,高度:34

这意味着它应该出现在左上角,但实际上没有。怎么了?

最佳答案

一个可能的原因是您直接使用了 titleLabel。考虑改用 setTitle:forState: 方法。

可以肯定的是,考虑将 backgroundColor 设置为调试步骤,以确保它出现。


编辑 正如其他人所建议的那样,尝试使用 buttonWithType: 而不是 [[UIButton alloc] init]。我建议使用 UIButtonTypeSystem 而不是 UIButtonTypeCustom

关于iOS:UIButton 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26635302/

相关文章:

ios - 在哪里使用 popToRootViewController 将额外的 VC 弹出堆栈?

ios - Objective-C 中的@synchronized

Objective-C Mac OSX 应用程序 - 从另一个委托(delegate)获取变量?

objective-c - 避免 UIKeyboard 和自动布局

iphone - 使用属性访问 init 中的 iVars?

ios - iScroll 在 iOS 设备上不工作

ios - Windows下制作CSR证书(7)

ios - NSFetchedResultsController indexPathForObject 不计算部分

ios - 在 opengles 中渲染三角形

iphone - 重用 UITableViewCell 时遇到问题