以编程方式调整从XIB自定义的UIView [英] Programmatically resize custom UIView from XIB

查看:238
本文介绍了以编程方式调整从XIB自定义的UIView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:我创建了一个自定义的的UIView 类及其在布局厦门国际银行文件。比方说,在厦门国际银行我自认为,大小为150×50我已启用 sizeClasses (温妮HANY)和自动版式。在模拟度量我已设置尺寸=自由形式状态栏=无,所有的其它=推断

I have following problem: I have created a custom UIView class and its layout in XIB file. Let's say that size of my custom view in XIB is 150 x 50. I have enabled sizeClasses (wAny hAny) and AutoLayout. In Simulated Metrics I have set Size = Freeform, Status Bar = None, all other = Inferred.

code 的UIView 类看起来是这样的:

Code from my custom UIView class looks like this:

#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

在我的的UIViewController 在这里我要添加这个定制的UIView ,我做了假的UIView(其大小我已经在Interface Builder中设置),我想加我的 CustomView 我想,我的 CustomView 拟合我的容器视图的边界

In my UIViewController where I want to add this custom UIView, I have made dummy UIView (which size I have set in Interface Builder) where I want to add my CustomView and I would like that my CustomView fits the bounds of my container view.

我想是这样做的:

_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]];

但没有运气。比方说,我的容器视图为300×100,当我我自定义的的UIView 添加到它,我的自定义视图仍然是150×50 <​​/ STRONG>。

But no luck. Let's say that my container view is 300 x 100, when I add my custom UIView to it, my custom view is still 150 x 50.

我曾尝试没有容器视图的工作 - 直接添加我的自定义的UIView 对象到我的的UIViewController 并设置它的宽度和高度:

I have tried to work without container view - to add my custom UIView object directly to self.view of my UIViewController and set it's width and height with:


  • 的autoLayout 限制

  • 使用 initWithFrame 初始化我的自定义时,的UIView

  • autoLayout constraints
  • by using initWithFrame when initializing my custom UIView

但同样 - 没有运气。自定义视图始终是150×50。

but again - no luck. Custom view is always 150 x 50.

有人能向我解释,我怎么能以编程方式添加我的自定义的UIView 厦门国际银行取得和调整它使用的autoLayout 限制

Can someone explain to me how can I programmatically add my custom UIView made in XIB and resize it using autoLayout constraints?

提前非常感谢。

编辑#1:错误试图在我的CustomView

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 )>

编辑#2:它的工作原理

view.translatesAutoresizingMaskIntoConstraints = NO;

在他的:

- (void) stretchToSuperView:(UIView*) view;

方法,一切工作就像我的预期。

method, everything works like I expected.

感谢@Andrea。

推荐答案

我想,主要的问题是,当您添加到厦门国际银行的内容视图中,您不使用自动布局。结果
在你的init方法添加子视图后,请调用该方法,将厦门国际银行的看法:

I guess that the main issue is that you do not use auto layout when you add your xib into the content view.
After adding the subview in you init methods please call that method, passing the view of the 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];
    }

}

结果
您设置code看起来应该是:
结果


Your setup code should look like that:

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

注重这里面伸观点我已经添加了 view.translatesAutoresizingMaskIntoConstraints = NO;
结果
结果
我会尽力来阐述我的答案的要求。使用自动版式,当你添加一个视图,而不设置约束自动布局自动自动尺寸调整口罩转换成约束他们的默认属性是 UIViewAutoresizingNone 这意味着不自动调整。结果
您XIB视图加入其父没有限制,并且没有因此自动调整大小保持其原始大小的可能性。由于您希望您的视图相应地调整你的观点,你有两种选择:

Pay attention that inside stretch view I've added view.translatesAutoresizingMaskIntoConstraints = NO;

I'll try to elaborate my answer as requested. Using autolayout, when you add a view without setting constraints autolayout automatically converts autoresizing masks into constraints the default property of them is UIViewAutoresizingNone that means to do not autoresize.
Your XIB view was added to its parent without constraints and without the possibility of autoresize thus keeping its original size. Since you want that your view to resize accordingly to your view you had two choices:


  • 更改您的视图厦门国际银行灵活的宽度和高度的自动尺寸调整面具,但他们需要匹配父大小或者你会不会有一个完整的封面

  • 添加一些约束约束两个视图相应地改变父的变化。你实现了厦门国际银行视图与其父之间的话说查看尾随/领导和顶部之间的空间/钮为0。就好像是你把一些胶水其边界。要做到这一点,你需要的autotranslate调整面具套入约束没有,或者你可以有一些冲突,你在日志中发布。
    搜索结果

你在做什么更高版本添加约束到视图(承载XIB视图),以它的父,但也有厦门国际银行在与其父之间没有约束,从而自动布局不知道如何来调整厦门国际银行视图。在视图层次结果每一个意见都应该有自己的约束。结果希望这有助于更好地理解。

What you are doing later is adding constraints to the view (that hosts the xib view) to its superview, but there were no constraints between the xib and its parent, thus autolayout doesn't know how to resize the xib view.
Each views in a view hierarchy should have its own constraints.
Hope this helps to understand better.

这篇关于以编程方式调整从XIB自定义的UIView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆