UIBarButtonItem: setBackgroundVerticalPositionAdjustment does not work properly using custom button

I have the following code to configure the display of UIBarButtonItem (locationButton):

UIButton *locationButtonAux = [UIButton buttonWithType:UIButtonTypeCustom]; [locationButtonAux setFrame:CGRectMake(0, 0, LOCATION_BUTTON_WIDTH, LOCATION_BUTTON_HEIGHT)]; [locationButtonAux setBackgroundImage:[UIImage imageNamed:@"location_button.png"] forState:UIControlStateNormal]; [locationButtonAux setBackgroundImage:[UIImage imageNamed:@"location_button.png"] forState:UIControlStateHighlighted]; [locationButtonAux addTarget:self action:@selector(userLocation) forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithCustomView:locationButtonAux]; UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithTitle:@"title" style:UIBarButtonItemStyleDone target:nil action:@selector(someMssage)]; [locationButton setBackgroundVerticalPositionAdjustment:-20.0 forBarMetrics:UIBarMetricsDefault]; self.navigationItem.rightBarButtonItem = locationButton; 

And I want to change the position of this button in the NavigationBar, because my NavigationBar is higher than usual (configured using Appeareance).

I am using the setBackgroundVerticalPositionAdjustment method, as you can see in the code, for this. But it doesn’t work with my UIBarButtonItem at all, no matter what offset I put there, the button is always displayed close to the bottom of the panel (snapshot). BUT, if I use the usual UIBarButtonItem with the usual style (the one that I called the button), I can see how the button position will be changed to an offset of -20. Very strange ... Any ideas?

Thanks!

enter image description here

+4
source share
2 answers

Perhaps something like this will work for you:

 UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 30)]; //change this frame to counteract for your taller navbar [rightView addSubview:locationButtonAux]; UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightView]; self.navigationItem.rightBarButtonItem = rightBarButtonItem; 

Hope this helps

+3
source

You need to put the button in an empty view and place it in this empty view. Now make it so that you can view the custom view of the panel item.

You really don't need a button here, as it is just an image, and you are not using any of the button's features (you can attach a gesture recognizer to notice a click). So here is a simple solution using image representation:

 UIImageView *locationButtonAux = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"location_button.png"]]; UIView* v = [[UIView alloc] initWithFrame:locationButtonAux.frame]; [v addSubview:locationButtonAux]; CGRect f = locationButtonAux.frame; f.origin.y -= 10; locationButtonAux.frame = f; UIBarButtonItem *locationButton = [[UIBarButtonItem alloc] initWithCustomView:v]; self.navigationItem.rightBarButtonItem = locationButton; 

I missed the gesture recognition part, but you can easily see how to add it.

+1
source

Source: https://habr.com/ru/post/1479769/


All Articles