[英]How to change UIView constraints based on device orientation using Objective C?
我正在嘗試創建具有portrait
和landscape
多個視圖的iOS
storyboard
。 我創建的所有故事圖板constraints
和自動autolayout
。
現在我的問題是肖像,我需要像下面的肖像圖像一樣顯示。 那就是我通過使用情節提要約束來完成的。
對於風景,只需要顯示其他我已被硬編碼隱藏的“ First View (red)
和底部欄。 但是我不知道如何將紅色屏幕首先擴展到底部的頂部。
注意:這是通用應用程序。
- (void) orientationChanged:(NSNotification *)note
{
UIDevice * device = note.object;
switch(device.orientation)
{
case UIDeviceOrientationPortrait:
/* start special animation */
NSLog(@"Portrait");
self.namelist_tableview.hidden = NO;// ORANGE VIEW
break;
case UIDeviceOrientationLandscapeRight:
/* start special animation */
NSLog(@"Landscape");
self.namelist_tableview.hidden = YES;// ORANGE VIEW
break;
case UIDeviceOrientationLandscapeLeft:
/* start special animation */
NSLog(@"Landscape");
self.namelist_tableview.hidden = YES;// ORANGE VIEW
break;
default:
break;
};
}
您可以添加紅色視圖高度約束,並將其存儲在UIViewController
。 當設備的方向改變時。 如果為縱向,請將后視視圖高度限制設置為等於您要設置的時間,橫向將紅色視圖高度限制設置為初始值。
非常簡單,在intefacebuilder中為縱向模式添加約束,為橫向模式添加約束。 接下來,為所有約束建立出口。 在設備更改方向事件中執行此操作
如果您處於橫向模式
+將肖像的活動約束設置為false(constraint.active = NO)
+將激活的景觀約束設置為true(constraint.active = YES)
如果您處於人像模式
+將激活的景觀約束設置為false(constraint.active = NO)
+將激活的肖像約束設置為true(constraint.active = YES)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.