使用自动布局使用IB_DESIGNABLE自定义视图错位视图警告和奇怪行为

时间:2015-03-20 13:42:29

标签: ios xcode6 autolayout ibdesignable

我创建了一个自定义IB可设计视图(请参阅下面的代码),它在IB中正确呈现,并且在运行时也可以正常工作。但是,在获取有关错误放置视图的警告时,我无法在Interface Builder中手动调整视图大小(触摸调整大小手柄时,视图将在其容器中跳转)。

我对各种不同的布局都有相同或类似的行为。你知道我在这里做错了什么,或者这只是IB中的一个错误?

(PS:我不能忽视警告)

Misplaced view warning

编辑:添加了约束的截图:

Constraints

这是代码(标题):



    IB_DESIGNABLE
    @interface AKATestView : UIView
    @end

实现:



    @interface AKATestView()

    @property(nonatomic)BOOL subviewsCreated;
    @property(nonatomic)BOOL subviewConstraintsCreated;
    @property(nonatomic)NSDictionary* views;

    @end

    @implementation AKATestView

    - (id)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super initWithCoder:aDecoder];
        if (self) {
            [self setupAfterInit];
        }
        return self;
    }
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setupAfterInit];
        }
        return self;
    }

    - (void)setupAfterInit
    {
        [self createSubviews];
    }

    - (void)createSubviews
    {
        if (!self.subviewsCreated)
        {
            self.translatesAutoresizingMaskIntoConstraints = NO;

            UILabel* labelView = [[UILabel alloc] initWithFrame:CGRectZero];
            labelView.text = @"Name";
            labelView.translatesAutoresizingMaskIntoConstraints = NO;
            [self addSubview:labelView];

            UITextField* textField = [[UITextField alloc] initWithFrame:CGRectZero];
            textField.borderStyle = UITextBorderStyleRoundedRect;
            textField.placeholder = @"Enter some text";
            textField.translatesAutoresizingMaskIntoConstraints = NO;
            [self addSubview:textField];

            UILabel* errorMessageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
            errorMessageLabel.text = @"Error message";
            errorMessageLabel.translatesAutoresizingMaskIntoConstraints = NO;
            [self addSubview:errorMessageLabel];

            self.views = @{ @"label": labelView, @"editor": textField, @"errorMessageLabel": errorMessageLabel };
            self.subviewsCreated = YES;

            [self setNeedsUpdateConstraints];
        }
    }

    - (void)updateConstraints
    {
        if (!self.subviewConstraintsCreated)
        {
            NSDictionary* metrics =
            @{ @"pt": @(4), @"pr": @(4), @"pb": @(4), @"pl": @(4),
               @"labelWidth": @(100),
               @"errorPl": @(4 + 100 + 4),
               @"hsLabelEditor": @(4), @"vsEditorError": @(2)
               };
            NSArray* specs =
            @[ @{ @"format": @"H:|-(pl)-[label(labelWidth)]-(hsLabelEditor)-[editor]-(pr)-|",
                  @"options": @(NSLayoutFormatAlignAllFirstBaseline) },
               @{ @"format": @"V:|-(pt)-[editor]-(vsEditorError)-[errorMessageLabel]-(pb)-|",
                  @"options": @(NSLayoutFormatAlignAllLeading|NSLayoutFormatAlignAllTrailing) }
               ];
            for (NSDictionary* spec in specs)
            {
                NSString* format = spec[@"format"];
                NSUInteger options = ((NSNumber*)spec[@"options"]).unsignedIntegerValue;
                NSArray* constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
                                                                               options:options
                                                                               metrics:metrics
                                                                                 views:self.views];
                [self addConstraints:constraints];
            }

            self.subviewConstraintsCreated = YES;
        }
        [super updateConstraints];
    }

    @end

1 个答案:

答案 0 :(得分:8)

尝试删除self.translatesAutoresizingMaskIntoConstraints = NO;方法中的createSubviews。 IB似乎依靠这种翻译来对设计师进行正确的测量。我有完全相同的问题,这解决了它。

对于子视图,我仍然translatesAutosizingMaskIntoConstraintsNO。我确认即使将此设置为YES,也不会产生任何额外的约束。希望你的情况也是如此!