我试图以编程方式将标签 UI 添加到我的 Objective-C View Controller 中,但编码后没有任何显示,下面是我的代码:
`
//
// ViewController.m
// ObjectiveWebView
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 70)];
[yourLabel setTextColor:[UIColor brownColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
[self.view addSubview:yourLabel];
}
- (void)viewDidAppear:(BOOL)animated{
NSLog(@"viewDidAppear loaded successfully");
}
@end
`
我做错了什么?
最佳答案
永远不会设置标签的文本。默认情况下,它是空的。因此,在您的情况下,标签已添加到 View 中,但由于未设置文本而不可见。尝试设置它。此外,如果 ViewController 嵌入到 NavigationController 中,则标签可能已隐藏在导航栏下方。尝试如下更改标签的 y 位置。
UILabel *yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 300, 70)];
[yourLabel setTextColor:[UIColor brownColor]];
[yourLabel setBackgroundColor:[UIColor clearColor]];
[yourLabel setFont:[UIFont fontWithName: @"Trebuchet MS" size: 14.0f]];
[yourLabel setText:@"Hello"];
[self.view addSubview:yourLabel];
关于ios - 以编程方式向 objective-c ios 应用程序添加标签不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59229081/