ios - 以编程方式向 objective-c ios 应用程序添加标签不显示

标签 ios objective-c

我试图以编程方式将标签 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/

相关文章:

ios - iPhone 4 iOS 7 滚动时 Tableview 滞后

ios - AWS 将区域从 East1 切换到 West2

iphone - 没有平滑的图像查看器

iphone - 带有自定义 map 的 MapKit

iphone - 必须存储太多图像文件

ios - 如何获取导航栏的高度

ios - 将数据从 UIViewController 传递到 UIPopover

ios - 在模态页面表上呈现模态表单

ios - 保存照片到相册iOS,不要全部保存

objective-c - 使用 OS X 的 Storyboard初始化另一个窗口