On iOS, how can I shift the navigationItem.leftBarButtonItem horizontally to the right?

The Cusomized UINavigationBar requires me to present the custom back button I use navigationItem.leftBarButtonItem = myCustomizedButton, but its position is fixed.

Can anyone please share so kindly how can I change this button 40 pixels to the right?

+3
source share
2 answers

You can create a containing view that is 40 pixels larger than your image. Add an image with an offset of 40 pixels. Add containing view as leftBarButtonItem.

Code follows:

// Create a containing view to position the button
UIView *containingView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, barButtonImage.size.width + 40, barButtonImage.size.height)] autorelease];

// Create a custom button with the image
UIButton *barUIButton = [UIButton buttonWithType:UIButtonTypeCustom];
[barUIButton setImage:barButtonImage forState:UIControlStateNormal];
barUIButton.frame = CGRectMake(40, 0, barButtonImage.size.width, barButtonImage.size.height);
[barUIButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

[containingView addSubview:barUIButton];

// Create a container bar button
UIBarButtonItem *containingBarButton = [[[UIBarButtonItem alloc] initWithCustomView:containingView] autorelease];

// Add the container bar button
navigationItem.leftBarButtonItem = containingBarButton;
+7
source

, navBar. , , . , ...

+1

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


All Articles