从纵向切换到横向时高度没有更新?

Height did not update when switch from portrait to Landscape?

我创建了一个 UIScrollView,在其中添加了其他数据,在 self.view 中添加了子视图。问题是,当我将 phone 从纵向切换为 横向 或反之时,它不会更新高度,因此我的 scrollview 无法正常工作,因为我需要.这是代码:

UIScrollView *menuScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 44+28+8, 130, self.view.frame.size.height)];

重写 ViewController 上的 didRotateFromInterfaceOrientation 方法并更改 scrollView.

的框架
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
    [super didRotateFromInterfaceOrientation:fromInterfaceOrientation];

    self.menuScrollView.frame=CGRectMake(0,yPosition,width,self.view.frame.size.height);
}

滚动视图不会更改其大小,因为您根本没有设置任何约束。

有两种方法可以做到这一点。第一个也是非常简单的一个,是在 Interface Builder 中创建滚动视图及其子视图,并在那里设置所有必要的约束。第二种更难的方法是以编程方式设置它们,但如果您不愿意这样做,那么您应该坚持第一个选项并在您的故事板(或 nib 文件)中完成所有工作。

就我个人而言,我会选择第一种方法,我会创建 IBOutlet 属性,以便稍后在代码中以编程方式设置子视图的任何其他属性。