2014-10-09 69 views
1

我有一個名爲LoginViewController的ViewController,它包含幾個文本字段和一個按鈕。自動佈局視圖控制器繼承

所有元素都與自動佈局(編程規則生成的規則)很好地工作,在這種情況下,元素以其超視圖爲中心在X.

現在,我想創建一個UIViewController,它從LoginViewController中調用LeftLoginViewController。這個LeftLoginViewController具有與LoginViewController相同的功能,但外觀會發生變化,它應該支持超級視圖左側的所有元素。

(這leftLoginViewController是一個例子,我的問題的重點是我該怎麼辦,我想移動在父親的看法視覺元素的情況)

什麼是實現這一目標的最佳方式是什麼?修改自動佈局規則?因爲顯然你不能修改框架。

我應該爲在parentViewController的主視圖中添加的每個規則創建屬性嗎?和LeftLoginViewController可以修改它們的常量?如果我想要將其中一個規則(如NSLayoutAttributeCenterX)更改爲NSLayoutAttributeTrailing,那麼會發生什麼情況,該屬性是隻讀的。

對不起,但我對此很困惑,我需要一些想法才能找到正確的方向。

回答

0

如果LeftLoginViewController的唯一區別在於它的佈局,則不需要子類。你可以用一個類型來初始化你的視圖控制器。

假設你正在使用Objective-C:

ViewController.h

typedef NS_ENUM(NSUInteger, LayoutType) { 
    LayoutTypeNormal, 
    LayoutTypeLeft 
}; 

@interface ViewController : UIViewController 

- (id)initWithLayoutType:(LayoutType)layoutType; 

@end 

ViewController.m

@interface ViewController() 

@property (nonatomic) LayoutType layoutType; 

@end 

@implementation ViewController 

- (id)initWithLayoutType:(LayoutType)layoutType 
{ 
    self = [super init]; 
    if (self) 
    { 
     self.layoutType = layoutType; 
    } 

    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    if (self.layoutType == LayoutTypeNormal) 
    { 
     // centre layout for normal 
    } 
    else if (self.layoutType == LayoutTypeLeft) 
    { 
     // left layout 
    } 
} 
+0

好了,所有的元素向左移動了一個例子,我意味着我可以如何實現從fatherViewController中移動元素 – xarly 2014-10-09 12:53:15