Ios 如何在nib中应用UIView的自动布局?

Ios 如何在nib中应用UIView的自动布局?,ios,objective-c,uiview,nib,ios-autolayout,Ios,Objective C,Uiview,Nib,Ios Autolayout,我对xib/nib上的自动布局感到困惑。我对故事板自动布局很熟悉 我想做的是: 1。我有一个故事板,其中有一个视图控制器和一个UIView,称为borderView。它被正确地约束。它被正确地调整了iphone 5和6的大小。以下是故事板的屏幕截图: 这是iphone 5中的边框视图:(我正在尝试将nib视图作为子视图添加到此边框视图) 2.我创建了一个带有UIView的笔尖,尺寸为300 x 300。我希望将此视图添加到我的ViewController中的borderVIew。这是我笔尖的

我对xib/nib上的自动布局感到困惑。我对故事板自动布局很熟悉

我想做的是:

1。我有一个故事板,其中有一个视图控制器和一个UIView,称为borderView。它被正确地约束。它被正确地调整了iphone 5和6的大小。以下是故事板的屏幕截图:

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

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

注意:我没有给出任何高度或宽度限制。我只是给出了引导,移动,顶部和底部

我正在尝试将笔尖添加到我的borderview中,如下所示:

这是我的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中执行,或者我需要以编程方式给出约束吗?

使用故事板而不是xib和故事板,并在您的borderview中添加另一个视图(包含xib中的所有内容)并将该视图约束在您的borderview中。 使用垂直居中和水平居中约束使内部视图居中。如果以后需要更改约束,请在头文件中为它们创建一个出口并调整优先级。
如果您仍然希望在视图中使用xib,请尝试使用第三方库(如Massy)来调整xib。

使用序列图像板而不是xib和序列图像板,然后在borderview中添加另一个视图(包含xib中的所有内容)并将该视图约束在borderview中。 使用垂直居中和水平居中约束使内部视图居中。如果以后需要更改约束,请在头文件中为它们创建一个出口并调整优先级。
如果仍要在视图中使用xib,请尝试使用第三方库(如Mashine)来调整xib。

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

如果您别无选择,只能通过这种方式尝试手动设置中心。i、 e.yourView.frame.x=super.frame.size.width/2-yourView.frame.size.width/2。这应该使它水平居中。如果不允许,则必须在添加到子视图后添加约束。

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

步骤1:为大小设置一些宏

#define kAlertViewWidth 300
#define kAlertViewHeight 300
@interface中的步骤2。请在viewcontroller中创建类似于此的属性

@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方法中的步骤3

 -(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\]\]\];
    }

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

步骤1:为大小设置一些宏

#define kAlertViewWidth 300
#define kAlertViewHeight 300
@interface中的步骤2。请在viewcontroller中创建类似于此的属性

@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方法中的步骤3

 -(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\]\]\];
    }