[英]How do I programmatically create views based on device orientation in iOS?
我想以編程方式創建許多視圖,並根據設備方向設置它們的大小。 這就是我現在正在做的事情:
UIView *view1 = [[UIView alloc] init];
if(UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
view1.frame = CGRectMake (0, 0, 1024, 44);
else
view1.frame = CGRectMake (0, 0, 764, 44);
[self.view addSubview:view1];
但是為每個視圖執行此操作很痛苦。 我還必須復制在- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation) toInterfaceOrientation duration:(NSTimeInterval)duration
方法中使用的代碼。 有沒有更簡單的方法可以做到這一點?
當然有
唯一要做的就是創建一個從UIViewController
繼承的BaseViewController
實現此基類的方法,您需要的所有其他ViewController
都繼承此BaseViewController
您還可以在此BaseViewController
配置UINavigationItems
和UIActivityIndicators
。
您必須適應面向對象的思想:)
嘿,你不必這樣做。 為視圖設置AutoResizingMask,在這里您可以指定不同的邊框,高度和寬度應如何改變父視圖的大小。
切勿嘗試手動更改它。 它將變得復雜..總是讓蘋果為您做...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.