首页 > 系统 > iOS > 正文

iOS 导航栏返回按钮自定义

2019-11-08 00:32:12
字体:
来源:转载
供稿:网友

如果app没有要求可以用系统的,不过很多app到时要求自定义返回按钮的,不过设置也比较简单 ,设置一个根UINavigationController,创建工程的时候设置的UINavigationController继承根UINavigationController就可以,然后在UINavigationController中有个-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated方法,我们在这里面设置就可以了

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{

    

    if (self.childViewControllers.count) { // 非根控制器

//这里面就是设置一个UIButton

                UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];        [backButton setTitle:@"返回" forState:UIControlStateNormal];        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];//        [backButton setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];        [backButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];        [backButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];                [backButton sizeToFit];                // 注意:一定要在按钮内容有尺寸的时候,设置才有效果        backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -25, 0, 0);                // 设置返回按钮        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];            }        // 这个方法才是真正执行跳转    [super pushViewController:viewController animated:animated];        

}

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表