0

the code in iOS 10 or earlier is worked

UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setTitle:@"yyyyy"  forState:UIControlStateNormal];
[btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn1 sizeToFit];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:btn1];

UIBarButtonItem *fixed = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixed.width = -22;

self.navigationItem.rightBarButtonItems  = @[fixed, item1];

enter image description here

if i want to do the same thing in iOS 11, what can i do fo it?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
CoderYL
  • 85
  • 7
  • What do you see in the iPhone 7 Plus running iOS 10? And why is the width negative? – rmaddy Dec 12 '17 at 16:46
  • Possible duplicate of [Negative spacer for UIBarButtonItem in navigation bar on iOS 11](https://stackoverflow.com/questions/45544961/negative-spacer-for-uibarbuttonitem-in-navigation-bar-on-ios-11) – pckill Dec 12 '17 at 16:55
  • @rmaddy because the code 'fixed.width = -22;' – CoderYL Dec 13 '17 at 01:48
  • Yes, I see that code, that's why I asked. Why are you setting the width to the negative value? What does that accomplish? And what happened when you want your app on the iPhone 7 Plus simulator with iOS 10? – rmaddy Dec 13 '17 at 01:52
  • @rmaddy i want to adjust the position of the 'yyyy' barbutton,in iOS10 ,i can do that, so i need help to do that in iOS 11. – CoderYL Dec 13 '17 at 02:08
  • @rmaddy, negative width used to work before iOS 11, it was used to adjust the position of the buttons (for example if you wanted them to have zero margin from the screen). – pckill Dec 13 '17 at 12:01

1 Answers1

0

We have acomplished this by subclassing UINavigationBar:

#import <UIKit/UIKit.h>

@interface InsetButtonsNavigationBar : UINavigationBar

@end

and setting layoutMargins

#import "InsetButtonsNavigationBar.h"

@implementation InsetButtonsNavigationBar

- (void)layoutSubviews {

    [super layoutSubviews];

    for (UIView * view in self.subviews) {

        view.layoutMargins = UIEdgeInsetsMake(0, 8, 0, 8);

    }

}

@end
Zuzana Paulis
  • 941
  • 1
  • 8
  • 19