简体   繁体   English

在loadView中设置UIView的backgroundColor无效

[英]Setting UIView's backgroundColor in loadView has no effect

I have a UIDatePicker in an iOS 7 app. 我在iOS 7应用中有一个UIDatePicker It's added to a view controller (subclass) and it shows up hardly readable because the background is black. 它被添加到视图控制器(子类)中,并且由于背景为黑色,因此几乎不可读。 (A date picker needs a light background.) (日期选择器需要浅色背景。)

- (void)loadView
{
    self.datePicker = [[UIDatePicker alloc] init];
    ...
    [self.view addSubview:self.datePicker];

    // When placed here, without viewDidAppear, background remains black/transparent
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // When places here, the background becomes indeed white.
    self.view.backgroundColor = [UIColor whiteColor];
}

I could of course set the background color of the date picker itself in loadView , and that's what I ended up doing. 我当然可以在loadView设置日期选择器本身的背景颜色,这就是我最终要做的。

But still I don't understand why setting the super view's background color in loadView did not work. 但是我仍然不明白为什么在loadView设置超级视图的背景颜色不起作用。

您应该使用-viewDidLoad而不是-loadView

As stated documentation the loadView is called when ever the viewcontrollers view property is accessed and the view property is nil . 如所述文档所述,只要访问viewcontrollers的view属性并且view属性为nil就会调用loadView If a NIB is associated with the controller his method wil load the view rom the NIB if not it will create a view. 如果NIB与控制器关联,则他的方法将从NIB加载视图,否则将创建视图。

The documentation also states that if you custom implementation should not call super, but this will require you to create your own view. 该文档还指出,如果您的自定义实现不应调用super,但这将需要您创建自己的视图。

self.view = [UIView new]

There is no self.view because you did not to call [super loadView]; 没有self.view因为您没有调用[super loadView]; , calling super will create the view for you. ,调用super将为您创建视图。 But you should create your own view if you are overriding this method. 但是,如果要覆盖此方法,则应创建自己的视图。 As stated in the documentation. 如文档中所述。

Beter is to use viewDidLoad this called when the view is loaded, either from NIB, storyboard or programmatically. 最好是在从NIB,情节提要或以编程方式加载视图时使用此调用的viewDidLoad

-(void) viewDidLoad{
    self.datePicker = [[UIDatePicker alloc] init];

    [self.view addSubview:self.datePicker];

    self.view.backgroundColor = [UIColor whiteColor];
}

You should probably be setting the background color in -viewWillAppear: . 您可能应该在-viewWillAppear:设置背景颜色。 From the docs for 来自的文档
-[UIViewController loadView] : -[UIViewController loadView]

The view controller calls this method when its view property is requested but is currently nil. 当请求其视图属性但当前为nil时,视图控制器将调用此方法。 This method loads or creates a view and assigns it to the view property. 此方法加载或创建视图,并将其分配给view属性。

I haven't tested it today , but I read this to say self.view is nil when -loadView is called, and until you set it explicitly. 今天我没有测试过,但我看是这样说的self.viewnil ,当-loadView被调用,直到你把它明确。 The main reason to override -loadView is to customize the UIViewController 's view property, presumably to use a specific subclass of UIView , rather than the generic. 覆盖-loadView是自定义UIViewControllerview属性,大概是使用UIView的特定子类,而不是泛型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM