sekaisan
sekaisan

Reputation: 441

How to adjust the vertical position of the button in the navigation bar?

I can adjust the height of my title in the navigation bar by:

    self.profileNavigationBar.setTitleVerticalPositionAdjustment(10, forBarMetrics: UIBarMetrics.Default)

But I cannot adjust the button in the navigation bar to fit the title position. enter image description here

The setting button is at an awkward position now. I tried

     self.navigationItem.rightBarButtonItem?.setTitlePositionAdjustment(UIOffsetMake(20, 10), forBarMetrics: UIBarMetrics.Default)
     self.navigationItem.rightBarButtonItem?.setBackButtonBackgroundVerticalPositionAdjustment(10, forBarMetrics: UIBarMetrics.Default)
     self.navigationItem.rightBarButtonItem?.setBackButtonTitlePositionAdjustment(UIOffsetMake(20, 20), forBarMetrics: UIBarMetrics.Default)

none of them works for me. Anyone has ideas? Thanks a lot!

Upvotes: 5

Views: 4774

Answers (3)

iGW
iGW

Reputation: 643

I found the solution of this problem by making adjustment in the Image Edge Insets of the custom button. I had the requirement in the app to increase the height of the navigation bar and after increasing the height makes the rightBarButtonItem and leftBarButtonItem images unpositioned problem.

Find the code below:-

UIImage *image = [[UIImage imageNamed:@"searchbar.png"];
UIButton* searchbutton = [UIButton buttonWithType:UIButtonTypeCustom];
[searchbutton addTarget:self action:@selector(searchBar:) forControlEvents:UIControlEventTouchUpInside]; 
searchbutton.frame = CGRectMake(0,0,22, 22);
[searchbutton setImage:image forState:UIControlStateNormal];
[searchbutton setImageEdgeInsets:UIEdgeInsetsMake(-50, 0,50, 0)];
// Make BarButton Item
 UIBarButtonItem *navItem = [[UIBarButtonItem alloc] initWithCustomView:searchbutton];
self.navigationItem.rightBarButtonItem = navItem;

Upvotes: 0

odm
odm

Reputation: 906

Try this to adjust the vertical position:

navigationItem.rightBarButtonItem?.setBackgroundVerticalPositionAdjustment(10.0, for: .default)

Upvotes: 2

sekaisan
sekaisan

Reputation: 441

I kind of figure it out.

Select the button in the storyboard, adjust the Edge Inset under Attributes Inspector.

Upvotes: 2

Related Questions