在任何尊重约束的设备上将nib文件缩放到全屏

时间:2015-06-03 09:25:04

标签: ios objective-c xib nib

我有一个名为"菜单"的.xib文件在我设计视图的文件中,然后通过执行以下操作来初始化类:

self.menuViewController = [[TWTMenuViewController alloc] initWithNibName:@"Menu" bundle:nil];

一切都很棒,因为设计是可视化的,但尺寸总是600x600。我怎么能这样做全屏并适应设备的屏幕大小?

nib使用autoLayout,它有约束。我希望它能够全尺寸并尊重约束。

1 个答案:

答案 0 :(得分:0)

为superview设置一些约束或更改Frame(旧方式)

override func didMoveToSuperview() {
    super.didMoveToSuperview()

    self.translatesAutoresizingMaskIntoConstraints = false

    NSLayoutConstraint(item: self.superview!, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0).active = true
    NSLayoutConstraint(item: self.superview!, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0).active = true
    NSLayoutConstraint(item: self.superview!, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0).active = true
    NSLayoutConstraint(item: self.superview!, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0).active = true
}

旧方式:

- (void)layoutSubviews
{
    CGRect fr = self.frame;
    fr.size = self.superview.frame.size;
    self.frame = fr;

    [super layoutSubviews];
}

现在,视图上的所有自动布局约束都可以正常工作。