The custom UIBarButtonItem Back button in the UINavigationBar needs to be moved to the left than it is by default. The code below shifts the image on the button, but not the button itself (see the image below).

How can I move not only the image, but the button itself?

button

const CGRect imageFrame = CGRectMake(0, 0, 44., 44.); UIButton *button = [[UIButton alloc] initWithFrame:imageFrame]; [button setImage:[UIImage imageNamed:@"backButton"] forState:UIControlStateNormal]; [button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; button.contentEdgeInsets = UIEdgeInsetsMake(0, -6., 0, 0); // здесь получается сдвинуть влево картинку на кнопке, но не саму кнопку // [button addTarget:self action:someSelector forControlEvents:UIControlEventTouchUpInside]; UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:button]; [self.navigationItem setLeftBarButtonItem:backButton]; 
  • You can try to use the answer given here - K. Davydenko
  • @ K.Davydenko has no effect ... in any case, iOS 11 - xhr

1 answer 1

From the last worker did this way:

 func findAndMoveButtonsStackView(_ testView: UIView?) { guard let view = testView, String(describing: type(of: view)) != "_UIButtonBarStackView" else { testView?.frame.origin.x -= 15 // на сколько хотим подвинуть return } view.subviews.forEach({ [weak self] in self?.findAndMoveButtonsStackView($0) }) } // и вызываем: findAndMoveButtonsStackView(navigationController?.navigationBar) 

Result:

enter image description here