简体   繁体   中英

how to add a subview to a view controller?

I have a green rectangle, but it won't display when i run the app. here is my code:

- (void) makeBlockg {

    x = arc4random()%(320) + 1;

    CGRect block = CGRectMake(x, 0, 100, 70);

    greenblock.frame = block;

    greenblock.self.backgroundColor = [UIColor greenColor];

    [self.view addSubview:greenblock];

}

This code works fine for me. Are you sure you are correctly initialising greenblock with greenblock = [[UIView alloc] init] ? Without doing this greenblock is nil, instead of being an empty UIView object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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