How to change the location of the back button on iOS?

You see that I am making a fully customizable navigation bar, and when I create the back button, it does not stay in the right place, as you can see under the code.

here is my application code

- (void)viewDidLoad
{
    [super viewDidLoad];
    if(self.navigationController.viewControllers.count > 1) {
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [backButton setTitle:@"" forState:UIControlStateNormal];
    [backButton setBackgroundImage:[UIImage imageNamed:@"backButton.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(didTapBackButton:) forControlEvents:UIControlEventTouchUpInside];
    backButton.frame = CGRectMake(0.0f, 0.0f, 44.0f, 45.0f);
    UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

    self.navigationItem.leftBarButtonItem = backButtonItem;
}

}

Pic

+5
source share
4 answers

I think you can just change the contentEdgeInsets to make the button position.

Something like that:

[backButton setContentEdgeInsets: UIEdgeInsetsMake (5, 0, -5, 0)];

Come from UIButton Class Reference

contentEdgeInsets

Insert or source fields for the rectangle surrounding the entire contents of the buttons.

@property (non-atomic) UIEdgeInsets contentEdgeInsets

. . (, , , ). , , . . UIEdgeInsetsMake . - UIEdgeInsetsZero.

+6

.

UIImage *origImage = [UIImage imageNamed:@"button-thin-hamburger.png"];
UIGraphicsBeginImageContextWithOptions(CGSizeMake(origImage.size.width + 10, origImage.size.height), NO, 0.0);
[origImage drawInRect:CGRectMake(10, 0, origImage.size.width, origImage.size.height)];
UIImage *buttonImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@""
                                                               style:UIBarButtonItemStylePlain
                                                              target:nil
                                                              action:nil];
self.navigationItem.backBarButtonItem = backButton;

[[UINavigationBar appearance] setBackIndicatorImage:buttonImage];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:buttonImage];
+3

NavigationBar...

    [self.navigationController setNavigationBarHidden:YES animated:NO];

UIButton..

    UIButton *btn1=[UIButton  buttonWithType:UIButtonTypeCustom];
    [btn1 setFrame:CGRectMake(10, 0, 60, 40)];// Frame u want

    [btn1 setImage:[UIImage imageNamed:@"BT_ipad_P_backbward@2x.png"] forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(Click_backward:) forControlEvents: UIControlEventTouchUpInside];
btn1.tintColor=[UIColor purpleColor];
    [self.view addSubview:btn1];
0
source

All Articles