Placing and Moving Views on Rotation (UIView)

前端 未结 4 853
失恋的感觉
失恋的感觉 2020-12-28 08:40

What\'s the \"correct\" way of exactly placing and moving views when an app rotates? That is, how can I have fine-grained control of the position, size, and reflow of my vie

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 09:24

    To have fine-grained control over the position and size of your subviews when the iPhone rotates, change the frame of the subviews in the UIViewController method willAnimateRotationToInterfaceOrientation:duration:. This method is called within an animation block, so all the changes to your subviews' frames that you make inside of it are animated.

    - (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
    {
        if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
            // Portrait frames
            self.subviewA.frame = CGRectMake(x, y, width, height);
            self.subviewB.frame = CGRectMake(x, y, width, height);
            self.subviewC.frame = CGRectMake(x, y, width, height);
        } else {
            // Landscape frames
            self.subviewA.frame = CGRectMake(x, y, width, height);
            self.subviewB.frame = CGRectMake(x, y, width, height);
            self.subviewC.frame = CGRectMake(x, y, width, height);
        }
    }
    

提交回复
热议问题