Auto layout, inserted view to fill the container view

允我心安 提交于 2019-12-14 03:52:35

问题


I'am trying to make the inserted view to fill the container view in which it is inserted, however iam getting

Invalid parameter not satisfying: [constraint isKindOfClass:[NSLayoutConstraint class]]

- (void)insertedView:(NSView *)insertedView needsToFillContainerView:(NSView *)containerView {

    [containerView addSubview:insertedView];
    [containerView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [containerView addConstraints:@[
                                    [NSLayoutConstraint constraintsWithVisualFormat:@"H:|[insertedView]|"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:NSDictionaryOfVariableBindings(insertedView)],
                                    [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[insertedView]|"
                                                                            options:0
                                                                            metrics:nil
                                                                              views:NSDictionaryOfVariableBindings(insertedView)]
                                    ]
    ];

}

Basically the one view i want to insert is loaded from a VC


回答1:


- (void)addSubview:(NSView *)insertedView fillingAndInsertedIntoView:(NSView *)containerView {

    [containerView addSubview:insertedView];
    [insertedView setTranslatesAutoresizingMaskIntoConstraints:NO];

    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[insertedView]|"
                                                                            options:0
                                                                            metrics:nil
                                                                            views:NSDictionaryOfVariableBindings(insertedView)]];
    [containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[insertedView]|"
                                                                          options:0
                                                                          metrics:nil
                                                                            views:NSDictionaryOfVariableBindings(insertedView)]];

    [containeView layoutIfNeeded];
}



回答2:


constraintsWithVisualFormat returns an NSArray.

@[] is the Objective C literal to create an NSArray.

So, here your method parameter for addConstraints is an NSArray with two elements, each of which is an NSArray.

That's an incorrect method parameter for addConstraints. It expects an NSArray of objects of type NSLayoutConstraint.

Changing your invocation to be along the lines of this will solve the problem:

[containerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat: etc.

Incidentally, I see so many posts about creating Auto Layout constraints in code. Interface Builder is far the superior method. As Erica Sadun so succinctly puts it in her book iOS Auto Layout Demystified:

Any views you lay out in Interface Builder are guaranteed to be satisfiable. You cannot create a wrong interface with inconsistent rules in IB. The same is not true in code.




回答3:


There are two methods in UIView

//parameter type is NSArray
-(void)addConstraints:(NSArray *) constraints

and

// parameter type is NSLayoutConstraint
-(void)addConstraint:(NSLayoutConstraint *) constraint

So,

constraintsWithVisualFormat 

will return a NSArray, you need use

addConstraints


来源:https://stackoverflow.com/questions/19004758/auto-layout-inserted-view-to-fill-the-container-view

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!