0

I have a button in my Navigation bar. This button is center aligned. When user taps on it a modal view is launched the same way than New section in Apple's Music app for iOS.

I want to use this down arrow picker image. I configured the image to be seen on right side (title - image) as per this StackOverflow question.

enter image description here

enter image description here

The problem happens when user picks another option from Modal view with a different text size: app updates button text which in some cases overlaps the image.

enter image description here

Is it any way to make this image right aligned in all cases, even when the text size changes? Thanks

Update: I am coding on Swift. Swift code snippets would be appreciated.

Community
  • 1
  • 1
Spacemonkey
  • 1,725
  • 3
  • 20
  • 44

2 Answers2

0

The titleView property of a UINavigationItem does not resizes itself based on its content. If you ever need the size to change you have to do it yourself.

You're best option is to use a custom view that changes its bounds when the content it displays changes to require a different size.

The other option I can think of would be to manually adjust the images left inset and the bounds of the button when the text changes. This requires you to calculate the width of the text to update the image left inset and the bounds.

Craig Siemens
  • 12,942
  • 1
  • 34
  • 51
  • i am agree abt create a custom view who resizes it's bounds based on chlid views but [self.navigationItem.titleView addSubview:containerView]; always added to top left/back bar button item. Can you hep here ? – maddy Nov 21 '15 at 16:07
  • You want to do `self.navigationItem.titleView = containerView` – Craig Siemens Nov 22 '15 at 03:38
0

for creating a custom view which grows based on child views you can do some thing like this:

UIView *superview = self.view;

UIView *containerView = [UIView new];
[containerView setBackgroundColor:[UIColor blackColor]];
[containerView setTranslatesAutoresizingMaskIntoConstraints:NO];
[superview addSubview:containerView];
//this will be containing my button and my label

UILabel *mylabel = [[UILabel alloc]init];
[mylabel setBackgroundColor:[UIColor redColor]];
[mylabel setTranslatesAutoresizingMaskIntoConstraints:NO];
mylabel.text = @"MyLabel";

UIButton *mybutton = [UIButton
                      buttonWithType:UIButtonTypeRoundedRect];
[mybutton setTitle:@"My Button ye ye yey yeyeyye yeyey"
          forState:UIControlStateNormal];
[mybutton setTranslatesAutoresizingMaskIntoConstraints:NO];
[mybutton setBackgroundColor:[UIColor greenColor]];

[containerView addSubview:mylabel];
[containerView addSubview:mybutton];


NSDictionary * views = NSDictionaryOfVariableBindings(mybutton,mylabel, containerView);
//create pin of fixed 2 pixes between UIButton and UILabel.Done
NSArray * horizontalConstraintsforbuttons = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-1-[mybutton(<=400)]-2-[mylabel(60)]-1-|" options:0 metrics:nil views:views];
NSArray * heightConstraintforbutton = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mybutton(==60)]" options:0 metrics:nil views:views];
NSArray * heightConstraintforLabel = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|[mylabel(==60)]" options:0 metrics:nil views:views];

[containerView addConstraints:horizontalConstraintsforbuttons];
[containerView addConstraints:heightConstraintforbutton];
[containerView addConstraints:heightConstraintforLabel];


//container view specific constraints

NSArray *heightConstraint = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[containerView(==60)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(containerView)];

[superview addConstraints:heightConstraint];


[superview addConstraint:[NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterX multiplier:1.0f constant:0.0]];

[superview addConstraint:[NSLayoutConstraint constraintWithItem:containerView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:superview attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0.0]];

OUTPUT

a customview which grows based on UIButton string length

you can take UIImageView in place of UILabel.

I can't figure out how to add this container view in navigation item tite view.

This is what final output if you add

[self.navigationItem.titleView addSubview:containerView]; 

at last line. tite view not centered

may be @clever Error can hep you

maddy
  • 4,001
  • 8
  • 42
  • 65