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

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

问题描述

我已经学会了如何与旋转仿射变换的景色(见这里 )。我也了解了自动布局(见<一href=\"http://stackoverflow.com/questions/17917437/ios-using-storyboard-and-autolayout-to-center-the-control/28446317#28446317\">here和的此处),甚至是程序自动布局(见的这里这里)。但是,我不知道如何让自动布局与一个旋转视图中工作。

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:

我觉得麻烦来自宽度和高度,因为旋转的变化。有没有什么办法让一个旋转视图填补它的的SuperView?有一些手段来得到自动布局工作或旋转后,它只是不兼容?

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?

(我只学会了斯威夫特,但我很高兴通过Objective-C的答案涉水如果这就是你更熟悉。)

(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.)

继@ VinayJain的建议,我做了以下内容:

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)
    


  • 但它是在这一点上,我意识到,我并不真的需要改变间距值。我想的间距保持为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:

    推荐答案

    您可以用自动布局做到这一点很容易,

    You can do it with autolayout easily,


    • 将钉扎的边缘,上海华
    • 创建约束
    • 创建这些限制出口(说leftSpace,rightSpace,topSpace,bottomSpace)

    • Create constraints by pinning the edges to superview
    • Create outlets for these constraints(say it leftSpace,rightSpace,topSpace,bottomSpace)

    检查旋转,并相应地更新这些值

    Check for rotation, and update these values accordingly

    使用此检查旋转:<一href=\"http://stackoverflow.com/questions/24166878/how-to-detect-orientation-change-in-custom-keyboard-extension-in-ios-8\">Check旋转

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

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