简体   繁体   English

自动布局视图控制器继承

[英]autolayout view controller inheritance

I have a ViewController called LoginViewController, and it holds a couple of textfields and a button. 我有一个名为LoginViewController的ViewController,它包含几个文本字段和一个按钮。

All elements are working great with auto layout (rules built programmatically), in this case the elements are centered at X with its superview. 所有元素都可以很好地使用自动布局(以编程方式构建的规则),在这种情况下,元素以其父视图以X为中心。

Now, I want to create a UIViewController which hesitance from LoginViewController, called LeftLoginViewController. 现在,我想创建一个从LoginViewController开始的UIViewController,称为LeftLoginViewController。 This LeftLoginViewController has the same functionality of LoginViewController, but the appearance changes, it should sustain all the elements to the left of the superview. 这个LeftLoginViewController具有与LoginViewController相同的功能,但是外观有所变化,它应该支持超级视图左侧的所有元素。

(This leftLoginViewController is an example, my question is focusing how should I manage a situation where I want to move visual elements in the father's view) (这个leftLoginViewController是一个示例,我的问题集中在如何处理要在父亲视图中移动视觉元素的情况)

What's the best way to accomplish this? 做到这一点的最佳方法是什么? To modify the autolayout rules? 要修改自动布局规则? Because obviously you cannot modify the frame. 因为显然您无法修改框架。

Should I create properties for each rule added in the main View of the parentViewController? 是否应该为添加到parentViewController主视图中的每个规则创建属性? and the LeftLoginViewController can modify their constant? 和LeftLoginViewController可以修改其常数吗? What happen if I want to change one of the rules like for example NSLayoutAttributeCenterX to NSLayoutAttributeTrailing, that property is read only. 如果我想将诸如NSLayoutAttributeCenterX之类的规则之一更改为NSLayoutAttributeTrailing,该属性是只读的,将会发生什么。

Sorry, but I'm very confused about this, I need some ideas to get the right direcction. 抱歉,但是我对此感到非常困惑,我需要一些想法来获得正确的方向。

If the only difference in LeftLoginViewController is its layout, you don't need a child class. 如果LeftLoginViewController的唯一区别是布局,则不需要子类。 You can init your view controller with a type. 您可以使用类型初始化视图控制器。

Assuming you're using Objective-C: 假设您使用的是Objective-C:

ViewController.h ViewController.h

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

@interface ViewController : UIViewController

- (id)initWithLayoutType:(LayoutType)layoutType;

@end

ViewController.m 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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM