5

I am having a strange problem with UIBarButtonItem. I am creating one with a custom view, and the view is of my own MyCustomView type. It contains couple of labels and some other subviews. Whenever I use autolayout in this custom view class to lay out the subviews - the button is displayed on top-left corner of the screen! You can see it in the picture below - MyCustomView just has one subview with gray background and I use autolayout to stretch it to fill the parent MyCustomView:

wrong

When I don't use autolayout everything is fine and the button is displayed normally:

right

Can anyone explain me what is going on here and am I allowed to use autolayout in custom views which are going to be put in UIBarButtonItems (maybe not on the topmost level) ?

UPDATE: this happens after I do

    self.translatesAutoresizingMaskIntoConstraints = NO;

in MyCustomView - in the top view which should be placed in bar button. I am doing it to make the system call my intrinsicContentSize method. I guess using the old sizeThatFits: instead would still be ok.

UPDATE 2: Here is the test code:

UIControl *cus = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
// uncomment the following line to mess everything
//cus.translatesAutoresizingMaskIntoConstraints = NO;
cus.backgroundColor = [UIColor redColor];
UILabel *label = [UILabel new];
label.translatesAutoresizingMaskIntoConstraints = NO;
label.text = @"Q";
[label sizeToFit];
label.font = [UIFont systemFontOfSize:18];
[cus addSubview:label];
NSLayoutConstraint *centerX1 = [NSLayoutConstraint constraintWithItem:cus
                                                            attribute:NSLayoutAttributeCenterX
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:label
                                                            attribute:NSLayoutAttributeCenterX
                                                           multiplier:1
                                                             constant:0];
NSLayoutConstraint *centerY1 = [NSLayoutConstraint constraintWithItem:cus
                                                            attribute:NSLayoutAttributeCenterY
                                                            relatedBy:NSLayoutRelationEqual
                                                               toItem:label
                                                            attribute:NSLayoutAttributeCenterY
                                                           multiplier:1
                                                             constant:0];
[cus addConstraints:@[centerX1, centerY1]];

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:cus];
controller.navigationItem.rightBarButtonItem = barButton;

I would be happy to get explanations to understand what is going on.

frangulyan
  • 2,790
  • 25
  • 50
  • Why would you create a UIBarButtonItem with a subview ? Can you tell me the reason you need to do so indeed? – Zigii Wong Nov 18 '15 at 01:54
  • Because I need a complex widget on navigation bar, with several labels, states and animations between them. Why do you ask? – frangulyan Nov 18 '15 at 21:22
  • Check this answer http://stackoverflow.com/a/15651197/488611 It looks like auto layout can't be used in a UINavigationBar or UIToolbar. – James P Jan 13 '16 at 11:29

0 Answers0