本文介绍了自动布局以全屏的旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个视频播放应用程序。该应用程序有大约一边看在纵向模式中的视频下方的应用程序的信息,但是当应用程序开启景观我想要的视频去全屏。不幸的是,当应用程序被打开,以景观底视图限制从顶部向底部扩大一路视频观看即使我已经变成了高度约束优先== 1。

I am writing a video playing app. The app has information about the app below the video while watching in portrait mode, but when the app is turned to landscape I want the video to go full screen. Unfortunately when the app is turned to landscape the bottom view constrains the video view from expanding all the way from the top to the bottom even though I've turned the height constraint to priority == 1.

人像模式:

Portrait Mode:

风景模式:

Landscape Mode:

我试着隐藏低的观点,但抛出整个布局走出低谷当我告诉在旋转回纵向视图,因为隐藏视图破坏与视图相关的限制。

I've tried hiding the lower view, but that throws the whole layout out of whack when I show the view on rotating back to portrait because hiding the view destroys the constraints associated with the view.

有没有一种方法我可以做一个单一视图占据整个屏幕轻松恢复到事情自动布局是很容易的方式吗?

Is there a way I can make a single view take up the entire screen easily and revert back to the way things were easily with Auto Layout?

感谢您。

推荐答案

是的,有一个简单的方法,但你需要做一些数学。您可以与您的视频观看到上海华的高度,使用乘法器和恒定性。你可以有两个等式确定你想要什么,以及解决的变量。例如,假设你希望你的身高是在纵向(在一个568点的高屏)225和全高,320,在景观。这就给了你这两个方程(文字,公式说:我希望我的高度相等的self.view的身高倍乘数加上一个常数):

Yes, there is an easy way, but you need to do a little math. You can relate the height of your video view to the superview, using the multiplier and constant properties. You can have two equations defining what you want, and solve for the variables. For example, say you want your height to be 225 in portrait (in a 568 point high screen), and full height, 320, in landscape. That gives you these two equations (in words, the equation says, "I want my height to equal self.view's height times a multiplier plus a constant"):

225 =568米+常数和320 =320米+恒

225 = 568m + constant and 320 = 320m + constant

如果你解决这两个等式,你会得到-0.3831 m和442.6为常数。
因此,在IB,你应该给你的视频观看高度约束,并作出一个IBOutlet它。其他视图也可以具有高度约束(但也有可能是其他的方式),但重要的一点是要以使它们能够连接在一起他们有垂直间距的限制给对方,并且没有约束到视图的底部。这样,当视频查看扩张,将推动关闭屏幕其他两个视图。在code,你可以这样做:

If you solve these two equations, you get -0.3831 for m and 442.6 for the constant.So, in IB, you should give your video view a height constraint, and make an IBOutlet to it. The other views could also have height constraints (but there are probably other ways), but the important point is you want them to have vertical spacing constraints to each other so they're tied together, and no constraint to the bottom of the view. That way, when the video view expands, it will push those other two views off the screen. In code, you would do this:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.topView removeConstraint:self.heightCon];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.topView attribute:NSLayoutAttributeHeight relatedBy:0 toItem:self.view attribute:NSLayoutAttributeHeight multiplier:-.3831 constant:442.6]];
}

heightCon是我的出口,以冠捷的(视频视图)高度约束。我们移除,然后与一种在self.view,适当涉及的两个高度取代它。如果你同时支持屏幕尺寸,你就必须在第一个方程用​​480代替568来求解方程,并基于设备上的正确的约束。有没有需要做什么旋转,约束系统处理,可自动

heightCon is my outlet to topView's (your video view) height constraint. We remove that, and then replace it with one in self.view that relates the two heights properly. If you're supporting both screen sizes, you would have to solve the equations using 480 instead of 568 in that first equation, and add the correct constraint based on the device. There's no need to do anything on rotation, the constraint system handles that automatically.

这篇关于自动布局以全屏的旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:39