【问题标题】:How to apply autolayout for UIView in nib?如何在 nib 中为 UIView 应用自动布局?
【发布时间】:2015-09-19 00:59:36
【问题描述】:

我对 xib/nib 上的自动布局感到困惑。我对故事板自动布局感到满意。

我想做的是:

1. 我有一个带有一个视图控制器和一个 UIView 的情节提要,称为borderView。它被正确约束。它为 iphone 5 和 6 正确调整大小。这是情节提要的屏幕截图:

这是 iphone 5 中的边框视图:(我正在尝试将 nib 视图作为子视图添加到此边框视图)

2. 我创建了一个带有 UIView 的笔尖,尺寸为 300 x 300。我希望将此视图添加到我的 ViewController 中的 borderVIew 中。这是我的笔尖的截图。

注意:我没有在任何地方给出任何高度或宽度限制。我只是给出了领先,traliing,顶部和底部。

我正在尝试将笔尖添加到我的边框视图中,如下所示:

这是我的 viewController 中的方法:

   -(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    [[NSBundle mainBundle] loadNibNamed:@"AlertView" owner:self options:nil];

    [self.borderView addSubview:self.myAlertViewFromNib];

}

在 iphone 5 中的结果是:

如您所见,nib 视图未在屏幕中心对齐(因为边框视图与屏幕中心对齐)。

我不知道如何对 nib 本身进行约束。谁能告诉我是否可以在 xcode 中执行此操作,或者我是否需要以编程方式给出约束?

【问题讨论】:

    标签: ios objective-c uiview nib ios-autolayout


    【解决方案1】:

    您希望将 AlertView 添加为视图控制器中的中心对齐 而不是从 xib 设置大小,只需创建完美的约束 xib 视图并为大小定义宏,如下所示。

    第 1 步有一些尺寸宏

    #define kAlertViewWidth 300
    #define kAlertViewHeight 300
    

    @interface 中的第 2 步。请在视图控制器中创建这样的属性

    @property (nonatomic, retain) IBOutlet UIView *myViewFromNib;
    

    属性的Getter方法

    -(UIView *)myViewFromNib
    {
    if (!_myViewFromNib) {
        _myViewFromNib = [[[NSBundle mainBundle] loadNibNamed:@"AlertView" owner:self options:nil] objectAtIndex:0];
        _myViewFromNib.translatesAutoresizingMaskIntoConstraints = NO;
    
    
    }
    return _myViewFromNib;
    }
    

    //viewDidLoad方法中的第三步

     -(void)viewDidLoad{
      [self setUpConstraints];
    }
    

    将添加所需约束的方法

        -(void)setUpConstraints
        {
            \[self.view addSubview:self.myViewFromNib\];
            \[NSLayoutConstraint activateConstraints:@\[\[NSLayoutConstraint constraintWithItem:self.myViewFromNib attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0\],
    
                                                      \[NSLayoutConstraint constraintWithItem:self.myViewFromNib attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0\],
    
                                                      \[NSLayoutConstraint constraintWithItem:self.myViewFromNib attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0
                                                                                    constant:kAlertViewWidth\],
    
                                                      \[NSLayoutConstraint constraintWithItem:self.myViewFromNib attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0
                                                                                    constant:kAlertViewHeight\]\]\];
        }
    

    【讨论】:

      【解决方案2】:

      如果您别无选择,只能尝试手动设置中心。即 yourView.frame.x = super.frame.size.width/2 - yourView.frame.size.width/2。这应该水平居中。如果它不允许你,那么你必须在添加到子视图后添加约束。

      【讨论】:

        【解决方案3】:

        不要使用 xib 和情节提要,而是使用情节提要并在您的边框视图中添加另一个视图(包含您在 xib 中的所有内容)并将该视图限制在您的边框视图中。 使用垂直居中和水平居中约束使内部视图居中。如果您以后需要更改约束,请在头文件中为它们创建一个插座并调整优先级。 如果您仍想在视图中使用 xib,请尝试使用 Masonry 等第三方库来调整您的 xib。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-08-25
          • 2016-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多