如何将UIView转换为横向和全屏?

时间:2017-02-17 03:51:30

标签: ios objective-c iphone

我想将UIView从纵向模式转换为横向模式,并将其设为全屏模式。我怎么能这样做?

CGFloat degrees = 90;
CGAffineTransform transform = CGAffineTransformMakeRotation((M_PI * degrees / 180.0f));

[UIView animateWithDuration:0.3f animations:^{
    transitionView.transform = transform;
    // How to set the origin to make it fullscreen?
} completion:^(BOOL finished) {
}];

3 个答案:

答案 0 :(得分:0)

enter image description here

您必须选择视图并为该视图提供约束,使其与边距一致(如下所示)。然后它会自动拉伸到全屏。

您还可以使用尺寸类,并为横向和纵向模式设置不同的约束。

答案 1 :(得分:0)

这里是swift 4中的解决方案

if UIDeviceOrientationIsLandscape(UIDevice.current.orientation){
        let screenSize = UIScreen.main.bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height
        videoView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
        playerLayer.frame = videoView.bounds
}

答案 2 :(得分:0)

  1. 通过交换高度和宽度来更改视图框
  2. 旋转-90度(逆时针旋转)
  3. 平移以匹配新的原点(因为围绕视图中心点执行旋转)

    UIView.animate(withDuration: 0.3) {
        self.frame = CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.height,
                                                                height: UIScreen.main.bounds.width))
    
        let rotate = CGAffineTransform(rotationAngle: -.pi / 2)
        let center = self.view.center
        let translate = CGAffineTransform(translationX: center.x + center.y - self.bounds.width,
                                          y: center.y - center.x)
        self.transform = translate.concatenating(rotate)
    }