简体   繁体   中英

Add UIScrollView to UIView programmatically not work in viewDidLoad but work in viewDidAppear?

I'm using autolayout.

I add a scrollview programmatically to uiview like the code below. I am trying to run initShopView in view did load but it just not work and not add the scrollview to view at all. I have see the view hierarchy capture.

class variables:

@property(strong,nonatomic) UIScrollView *shopScrollView;
@property(strong,nonatomic) UIView *headView;
@property(strong,nonatomic) UIButton *favoriteButton;


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initShopView];// not work

}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self initShopView];// will work
}

-(void)initShopView{
    self.shopScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.superview.frame.size.height - slideTitleHeight)];
    self.shopScrollView.contentSize = CGSizeMake(self.view.frame.size.width, 800);
    self.headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];

    self.favoriteButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.frame.size.width - 60, 10, 55, 55)];
    [self.favoriteButton setTitle:@"Favorite" forState:UIControlStateNormal];
    [self.favoriteButton setImage:[UIImage imageNamed:@"favoriteGreen.png"] forState:UIControlStateNormal];



    [self.headView addSubview:self.favoriteButton];
    [self.shopScrollView addSubview:self.headView];
    [self.view addSubview:self.shopScrollView];
}

@Phillip Mills give the solution. My scroll view's frame is

self.shopScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.superview.frame.size.height - slideTitleHeight)];

And the solution is:

self.shopScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - slideTitleHeight)];

In viewDidLoad , your view doesn't have a superview because it hasn't been inserted into the view hierarchy yet. That means you're setting a height of zero for your scroll view.

If you use Xcode's view debugging, you will see the scroll view in the list but with a "wrong" frame.

This is b'coz in viewDidAppear method you are getting the actual layout (frames) of a view.

To make work in viewDidLoad you need to call these method just above the [self initShopView]; method.

[self setNeedsLayout];

[self layoutIfNeeded];

Note: When you are using autolayout then it is not recommended to set frame of view. You just need to give constraints to view to place it on correct position.

Instead of creating frames, use AutoLayout for setting up your views. It will work in viewDidLoad as well then. Frames make the view rendering more complicated and do not work properly on all device sizes.

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