I have a view(320x480 also need to make it compatible with iphone5) in that view navigation bar (I have used Imageview for that) on right button click I want to show one view with 140x140 height and width so It will look something like right aligned to main view and just below the navigation bar (or we can say top of the view)

for that If I write below code, it does not show properly also my subview (which has buttons) is not clickable.

I don't have to use XIB in this case.I have to set this programmatically.

[self.containerView addSubview:settingsView];


    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:settingsView
                                                              attribute:NSLayoutAttributeTop
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.containerView
                                                              attribute:NSLayoutAttributeTop
                                                             multiplier:1.0
                                                               constant:0.0]];


    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:settingsView
                                                              attribute:NSLayoutAttributeRight
                                                              relatedBy:NSLayoutRelationEqual
                                                                 toItem:self.containerView
                                                              attribute:NSLayoutAttributeRight
                                                             multiplier:1.0
                                                               constant:0.0]];

    [self.containerView addConstraints:constraints];

Any idea with this? what is wrong?

有帮助吗?

解决方案

  1. Your layout is very likely ambiguous. You've only told the system to pin the view at the top and the right. It can satisfy those constraints by being a 1x1pt tiny box in the top right corner, but also by filling the entire superview. Therefore, the Auto Layout system also needs to know how to calculate the width and height of the subview before the view is sufficiently constrained.

    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:settingsView
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute: 0
                                                         multiplier:1.0
                                                           constant:140.0]];
    
    
    [containerView addConstraint:[NSLayoutConstraint constraintWithItem:settingsView
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:nil
                                                          attribute:0
                                                         multiplier:1.0
                                                           constant:140.0]];
    
  2. Make sure you set the translatesAutoresizingMaskIntoConstraints property of the subview to NO.

Try that and perhaps it will also fix your buttons.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top