改变self.view 在viewDidAppear、viewWillLayoutSubviews、viewDidLayoutSubviews方法中修改才能产生效果。
一个viewController的初始化大概涉及到如下几个方法的调用:
initWithNibName:bundle:{
初始化成员变量,做一些与view无关的事情
}
viewDidLoad
{
进行view布局相关性的调整:
比如:改变背景颜色、增加一些subView
}
viewWillAppear:animated:
{
viewDidAppear:animated:
viewWillLayoutSubviews
viewDidLayoutSubviews
改变self.view 在viewDidAppear、viewWillLayoutSubviews、viewDidLayoutSubviews方法中修改才能产生效果
}
所以在视图控制器里一般就在viewDidLayoutSubviews里改写view的frame。
代码demo如下:
- (void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
UIView *view = self.view.superview;
[self.view setFrame:view.bounds];
self.progressView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 3);
self.backBtn.frame = CGRectMake(self.view.bounds.size.width-40, 0, 40, 40);
self.webView.frame = self.view.bounds;
}
如果是view中的话,就用
- (void)layoutSubviews{
[super layoutSubviews];
UIView *view = self.view.superview;
[self.view setFrame:view.bounds];
self.progressView.frame = CGRectMake(0, 0, self.view.bounds.size.width, 3);
self.backBtn.frame = CGRectMake(self.view.bounds.size.width-40, 0, 40, 40);
self.webView.frame = self.view.bounds;
}