UIView不显示addSubview

Ter*_*oto 1 objective-c uiview ios

我描述了[self.view addSubView:myView];,
但myView没有显示self.view.

ViewController.h

@interface ViewController : UIViewController{
    UIView *myView;
}

@property (nonatomic, strong) UIView *myView;
Run Code Online (Sandbox Code Playgroud)

ViewController.m

@synthesize myView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    myView.frame = CGRectMake(-10, 70, 320, 480);
    [self.view addSubview:myView];
    myView.backgroundColor = [UIColor redColor];
    [self.view bringSubviewToFront:myView];
}
Run Code Online (Sandbox Code Playgroud)

你有什么解决办法?

Gre*_*reg 5

您需要先分配视图:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // If you use custom view change UIView to the custom class name
    myView = [[UIView alloc] initWithFrame:CGRectMake(-10, 70, 320, 480)];

    [self.view addSubview:myView];
    myView.backgroundColor = [UIColor redColor];
    [self.view bringSubviewToFront:myView];
}
Run Code Online (Sandbox Code Playgroud)