2016-03-03 2 views
3

Это мой кодИзменить положение ближе к правой части rightBarButtonItem в UINavigationBar

- (void)createCustomBtnRightBar:(UIImage *)buttonImage 
{ 
    self.navigationItem.rightBarButtonItem = nil; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    if (buttonImage != nil) { 
     [button setImage:buttonImage forState:UIControlStateNormal]; 
     button.frame = CGRectMake(0, 0, 45, 33); 
     button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -10); 
    } else { 
     [button setTitle:@"Add New Pal" forState:UIControlStateNormal]; 
     button.frame = CGRectMake(0, 0, 110, 24); 
     [button.titleLabel setTextAlignment:NSTextAlignmentRight]; 
    } 

    [button addTarget:self action:@selector(onClickBtnSendClip:) forControlEvents:UIControlEventTouchUpInside];  
    [button setBackgroundColor:[UIColor greenColor]]; 

    //create a UIBarButtonItem with the button as a custom view 
    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

    self.navigationItem.rightBarButtonItem = customBarItem; 
} 

enter image description here

Когда Изменение ширины до 200

enter image description here

Когда добавить button.titleEdgeInsets = UIEdgeInsetsMake(0, 20, 0, 0);

enter image description here

Во всех этих случаях, область права не может двигаться

мне нужно что-то вроде этого.

enter image description here

Я даже попробовал этот

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 
customBarItem.customView = button; 

но врезаться

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Fixed and flexible space items not allowed as individual navigation bar button item. Please use the rightBarButtonItems (that's plural) property.' 
*** First throw call stack: 
(
    0 CoreFoundation      0x00000001132a2e65 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x0000000112d19deb objc_exception_throw + 48 
    2 CoreFoundation      0x00000001132a2d9d +[NSException raise:format:] + 205 
    3 UIKit        0x000000011156d560 -[UINavigationItem setRightBarButtonItem:animated:] + 131 
    4 sdsd sdsdsd       0x000000010e042dd3 -[SendClipViewController createCustomBtnRightBar:] + 1475 

работал совершенным раньше, когда я была кнопка бар прямо из раскадровки и пространства на право было меньше (как мне нужно).

enter image description here

Любая помощь будет оценена. Благодаря

ответ

12

Вы должны использовать .Fixed пространство для достижения этой цели:

UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; 
negativeSpacer.width = -10; 
self.navigationItem.rightBarButtonItems = @[negativeSpacer, customBarItem]; 
+0

спасибо человека. Прекрасно работает. – jose920405

+0

Рад слышать, что –

+2

Используйте 'self.navigationController.view.layoutMargins.right' за фактический запас – BugaBuga