2017-12-12 4 views
0

der Code in iOS 10 oder früheriOS 11 - Wie ändere ich die Position von rightBarButtonItems?

arbeiten
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

wenn ich möchte die gleiche Sache in iOS 11, zu tun, was ich kann fo es tun?

+0

Was 10 iOS tun Sie im iPhone 7 Plus-sehen zu laufen? Und warum ist die Breite negativ? – rmaddy

+0

Mögliches Duplikat von [Negativer Platzhalter für UIBarButtonItem in der Navigationsleiste unter iOS 11] (https://stackoverflow.com/questions/45544961/negative-spacer-for-uibarbuttonitem-in-navigation-bar-on-ios-11) – pckill

+0

@rmaddy, weil der Code 'fixed.width = -22;' – CoderYL

Antwort

0

Wir haben diese acomplished UINavigationBar von Subklassen:

#import <UIKit/UIKit.h> 

@interface InsetButtonsNavigationBar : UINavigationBar 

@end 

und Einstellung layoutMargins

#import "InsetButtonsNavigationBar.h" 

@implementation InsetButtonsNavigationBar 

- (void)layoutSubviews { 

    [super layoutSubviews]; 

    for (UIView * view in self.subviews) { 

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

    } 

} 

@end 
Verwandte Themen