UIView不显示addSubview

时间:2014-02-27 09:49:13

标签: ios objective-c uiview

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

ViewController.h

@interface ViewController : UIViewController{
    UIView *myView;
}

@property (nonatomic, strong) UIView *myView;

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];
}

你有解决方案吗?

1 个答案:

答案 0 :(得分: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];
}