Making custom back button for navigation bar

后端 未结 1 1532
自闭症患者
自闭症患者 2021-01-26 08:07

I have made at least 10 buttons for my navigation bar, but it never seems to work right. The rounded edges get pixelated. I cant have that in an app, so can anyone tell me how t

相关标签:
1条回答
  • 2021-01-26 08:27

    Try this code:

    UIImage *backButtonImage = [UIImage imageNamed:@"backButton.png"];
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:backButtonImage forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, backButtonImage.size.width, backButtonImage.size.height);
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *customBackBarItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = customBackBarItem;
    

    and, the back method:

    -(void)back {
        [self.navigationController popViewControllerAnimated:YES];
    }
    

    The code above just set the image for normal state. You can also set highlighted state for a better appearance. Add some codes Like:

    UIImage *backButtonImageHighlighted = [UIImage imageNamed:@"backButtonHighlighted.png"];
    [backButton setImage:backButtonImageHighlighted forState:UIControlStateHighlighted];
    
    0 讨论(0)
提交回复
热议问题