I'm trying to add a drop down arrow or an icon next to the title of navigation bar (shown in screenshot below) but haven't found a good solution, I thought it would be fairly straight forward but I just can't get a good solution going.
One approach I tried is to replace the title with a UIButton
by setting the view controller's navigationItem.titleView
but the problem with this approach is since my title can vary in length I can't the button frame size calculated the CGRect
is reported as 0,0. If I try to update the button's frame in viewWillDisplay()
method then the button frame change is zoomed in animated in place and it's visible to the user and quite a jarring effect.
Are there any other possible solutions, I feel like I'm just approaching this all wrong, it shouldn't be this hard.
You can set a label with attributedstring,it will auto resize
Short title
Long title
For example
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,100, 40)];
label.textAlignment = NSTextAlignmentCenter;
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
NSTextAttachment * attach = [[NSTextAttachment alloc] init];
attach.image = [UIImage imageNamed:@"memoAccess"];
attach.bounds = CGRectMake(0, 0, 20, 20);
NSAttributedString * imageStr = [NSAttributedString attributedStringWithAttachment:attach];
NSMutableAttributedString * mutableAttriStr = [[NSMutableAttributedString alloc] initWithString:@"Title"];
[mutableAttriStr appendAttributedString:imageStr];
label.attributedText = mutableAttriStr;
self.navigationItem.titleView = label;
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments