以编程方式从XIB调整自定义UIView的大小

uer*_*ceg 14 resize objective-c uiview ios autolayout

我有以下问题:我已经UIViewXIB文件中创建了一个自定义类及其布局.假设我在XIB中的自定义视图的大小是150 x 50.我启用了sizeClasses(wAny hAny)和AutoLayout.在模拟度量我已经设置Size = Freeform,Status Bar = None所有other = Inferred.

我的自定义UIView类中的代码如下所示:

#import "CustomView.h"

@implementation CustomView

#pragma mark - Object lifecycle

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];

    if (self) {
        [self setup];
    }

    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];

    if (self) {
        [self setup];
    }

    return self;
}

#pragma mark - Private & helper methods

- (void)setup {
    if (self.subviews.count == 0) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
        self.bounds = self.view.bounds;
        [self addSubview:self.view];
    }
}

@end
Run Code Online (Sandbox Code Playgroud)

在我UIViewController想要添加这个自定义的地方UIView,我已经制作了虚拟UIView(我在Interface Builder中设置了它的大小),我想在其中添加我的内容CustomView,我希望我CustomView能够适应容器视图的界限.

我想这样做:

_vCustomView = [[CustomView alloc] init];
_vCustomView.translatesAutoresizingMaskIntoConstraints = NO;
[_vContainer addSubview:_vCustomView];

[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0]];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.0]];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.0]];
[_vContainer addConstraint:[NSLayoutConstraint constraintWithItem:_vCustomView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:_vContainer attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0]];
Run Code Online (Sandbox Code Playgroud)

但没有运气.假设我的容器视图是300 x 100,当我向其添加自定义UIView时,我的自定义视图仍然是150 x 50.

我试图在没有容器视图的情况下工作 - 将我的自定义UIView对象直接添加到我的self.view UIViewController并设置它的宽度和高度:

  • autoLayout 限制
  • initWithFrame在初始化我的自定义时使用UIView

但又一次 - 没有运气.自定义视图始终为150 x 50.

谁能向我解释我怎么能以编程方式添加我的自定义UIView在取得XIB和使用调整其大小autoLayout的限制

提前谢谢了.

编辑#1:我在我的CustomView上运行Andrea的代码时遇到的错误

Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSLayoutConstraint:0x17489b760 V:[UIView:0x17418d820(91)]>",
    "<NSLayoutConstraint:0x170c9bf80 V:|-(0)-[CustomView:0x1741da7c0]   (Names: '|':CustomView:0x1741da8b0 )>",
    "<NSLayoutConstraint:0x170c9bfd0 V:[CustomView:0x1741da7c0]-(0)-|   (Names: '|':CustomView:0x1741da8b0 )>",
    "<NSLayoutConstraint:0x170c9be90 V:|-(0)-[CustomView:0x1741da8b0]   (Names: '|':UIView:0x17418d820 )>",
    "<NSLayoutConstraint:0x170c9bee0 CustomView:0x1741da8b0.bottom == UIView:0x17418d820.bottom>",
    "<NSAutoresizingMaskLayoutConstraint:0x170c9dba0 h=--& v=--& CustomView:0x1741da7c0.midY == + 25.0>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170c9bfd0 V:[CustomView:0x1741da7c0]-(0)-|   (Names: '|':CustomView:0x1741da8b0 )>
Run Code Online (Sandbox Code Playgroud)

编辑#2:它有效!

@Andrea补充说:

view.translatesAutoresizingMaskIntoConstraints = NO;
Run Code Online (Sandbox Code Playgroud)

在他的:

- (void) stretchToSuperView:(UIView*) view;
Run Code Online (Sandbox Code Playgroud)

方法,一切都像我预期的那样.

感谢@Andrea.

And*_*rea 19

我想主要的问题是,当您将xib添加到内容视图时,不要使用自动布局.
在init方法中添加子视图后,请调用该方法,传递xib的视图:

- (void) stretchToSuperView:(UIView*) view {
    view.translatesAutoresizingMaskIntoConstraints = NO;
    NSDictionary *bindings = NSDictionaryOfVariableBindings(view);
    NSString *formatTemplate = @"%@:|[view]|";
    for (NSString * axis in @[@"H",@"V"]) {
        NSString * format = [NSString stringWithFormat:formatTemplate,axis];
        NSArray * constraints = [NSLayoutConstraint constraintsWithVisualFormat:format options:0 metrics:nil views:bindings];
        [view.superview addConstraints:constraints];
    }

}
Run Code Online (Sandbox Code Playgroud)

[编辑]
您的设置代码应如下所示:

- (void)setup {
    if (self.subviews.count == 0) {
        [[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil];
        self.bounds = self.view.bounds;
        [self addSubview:self.view];
        [self stretchToSuperView:self.view];
    }
}
Run Code Online (Sandbox Code Playgroud)

注意我在内部拉伸视图中添加了view.translatesAutoresizingMaskIntoConstraints = NO;
[编辑2]
我将尝试根据要求详细说明我的答案.使用autolayout,当您添加视图而不设置约束时,autolayout会自动将自动调整蒙版转换为约束,它们的默认属性就是UIViewAutoresizingNone不自动调整大小.
您的XIB视图已添加到其父级而没有约束,并且没有自动大小的可能性,因此保持其原始大小.由于您希望视图根据视图调整大小,因此您有两种选择:

  • 将xib视图的自动调整遮罩更改为灵活的宽度和高度,但它们需要与父级大小匹配,否则您将无法完整覆盖
  • 添加一些限制两个视图的约束,以相应地更改父更改.你在XIB视图和它的父视图之间实现了这个说法,尾随/前导和顶部/底部之间的空间是0.就像你在他们的边界上放了一些胶水.为此,您需要将自动转换大小调整掩码设置为NO,否则您可能会在日志中发布一些冲突.

您稍后要做的是将视图(承载XIB视图)的约束添加到其超级视图,但XIB与其父视图之间没有约束,因此autolayout不知道如何调整XIB视图的大小.
视图层次结构中的每个视图都应具有自己的约束.
希望这有助于更好地理解.

  • 这个答案可能会按预期工作,但在自定义XIB中将`self.bounds = self.view.bounds;`更改为`view.frame = self.bounds`会产生相同的效果,而不做其他任何事情.这是一个在Swift中说明这个想法的视频https://www.youtube.com/watch?v=H-55qZYc9qI (3认同)