旋转视图是否与自动布局兼容? [英] Are rotated Views compatible with Auto Layout?

查看:15
本文介绍了旋转视图是否与自动布局兼容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经学会了如何使用仿射变换旋转视图(请参阅 用于子视图四周的间距约束.

@IBOutlet 弱变量 rightSpace: NSLayoutConstraint!@IBOutlet 弱变量 leftSpace:NSLayoutConstraint!@IBOutlet 弱 var topSpace:NSLayoutConstraint!@IBOutlet weak var bottomSpace: NSLayoutConstraint!

  • 旋转子视图

    subview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))

  • 改变了网点的约束

    self.rightSpace.constant = CGFloat(0)self.leftSpace.constant = CGFloat(0)self.topSpace.constant = CGFloat(0)self.bottomSpace.constant = CGFloat(0)

  • 但正是在这一点上,我意识到我真的不需要更改间距值.我希望间距保持为 0.我只需要它自行调整.然而,旋转把这搞砸了.效果如下图所示:

    解决方案

    我在尝试通过 HDMI/AirPlay 从 iOS 运行外部视频到旋转的电视屏幕时遇到了这个问题.

    有时,电视会设置将信号旋转"90 度或 180 度等,但通常它没有该选项.

    使用这样的包装"视图,我可以强制外部屏幕上的视图控制器旋转其内容,而内容实际上并不知道有什么不同.

    让 contentsView = SomeWildCustomViewUsingAutoLayoutEtc()让 wrapper = RotatingWrapperView(contentsView)wrapper.translatesAutoresizingMaskIntoConstraints = falseparentView.addSubview(包装器)//...wrapper.rotation = .upsideDown

    RotatingWrapperView 的来源:https://github.com/quantcon/UIKit/blob/master/AutoLayoutRotationDemo/Views/RotatingWrapperView.swift

    I have learned how to rotate a view with Affine Transforms (see here). I have also learned about Auto Layout (see here and here), and even programmatic Auto Layout (see here and here). However, I don't know how to get Auto Layout to work with a rotated view.

    This image shows what I would like to do:

    I think the trouble comes from the width and the height changing because of the rotation. Is there any way to make a rotated view fill it's superview? Is there some trick to get Auto Layout to work or is it just incompatible after a rotation?

    (I've only learned Swift, but I'd be glad to wade through Objective-C answers if that is what you are more familiar with.)

    Update

    Following @VinayJain's suggestion I did the following:

    • pinned the subview edges to the superview in the storyboard.
    • created IBOutlets for the spacing constraints on all sides of the subview.

      @IBOutlet weak var rightSpace: NSLayoutConstraint!
      @IBOutlet weak var leftSpace: NSLayoutConstraint!
      @IBOutlet weak var topSpace: NSLayoutConstraint!
      @IBOutlet weak var bottomSpace: NSLayoutConstraint!
      

    • rotated the subview

      subview.transform = CGAffineTransformMakeRotation(CGFloat(M_PI_2))
      

    • changed the constraints from the outlets

      self.rightSpace.constant = CGFloat(0)
      self.leftSpace.constant = CGFloat(0)
      self.topSpace.constant = CGFloat(0)
      self.bottomSpace.constant = CGFloat(0)
      

    But it was at this point that I realized that I don't really need to change the spacing value. I want the spacing to remain 0. I just need it to adjust itself. However, the rotation messes that up. The effect is shown in the following image:

    解决方案

    I came across this problem when trying to run external video over HDMI / AirPlay from iOS to a rotated TV screen.

    Sometimes, the TV will have a setting to "rotate" the signal 90-degrees or 180-degrees, etc. but often times it will not have that option.

    With a "wrapper" view like this, I am able to force the view controller on the external screen to rotate its contents without the contents actually being aware that anything is different.

    let contentsView = SomeWildCustomViewUsingAutoLayoutEtc()
    let wrapper = RotatingWrapperView(contentsView)
    wrapper.translatesAutoresizingMaskIntoConstraints = false
    parentView.addSubview(wrapper)
    // ...
    wrapper.rotation = .upsideDown
    

    Source for RotatingWrapperView: https://github.com/quantcon/UIKit/blob/master/AutoLayoutRotationDemo/Views/RotatingWrapperView.swift

    这篇关于旋转视图是否与自动布局兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆