美文网首页
导航栏的定义操作

导航栏的定义操作

作者: 海笙樾 | 来源:发表于2018-03-27 15:38 被阅读0次

    导航栏设置透明

    UIImage *image = [[UIImage alloc] init];[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];

        //去掉透明后导航栏下边的黑边

        [self.navigationController.navigationBar setShadowImage:image];

        self.navigationController.navigationBar.barStyle = UIBarStyleDefault;

        self.navigationController.navigationBar.translucent = YES;

    导航栏背景自定义,去除下方的黑线

    @property (nonatomic, strong) UIImageView * navBarHairlineImageView;

    - (UIImageView *)findHairlineImageViewUnder:(UIView *)view {

        if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {

            return (UIImageView *)view;

        }

        for (UIView *subview in view.subviews) {

            UIImageView *imageView = [self findHairlineImageViewUnder:subview];

            if (imageView) {

                return imageView;

            }

        }

        return nil;

    }

    - (void)viewWillAppear:(BOOL)animated{

        _navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];

        _navBarHairlineImageView.hidden = YES;

        [self.navigationController.navigationBar setBarTintColor:你的颜色];

        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];

        self.tabBarController.tabBar.hidden = YES;

        [self navButton];

    }

    - (void)viewWillDisappear:(BOOL)animated{

        _navBarHairlineImageView.hidden = NO;

        [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithWhite:1 alpha:0.8]];

        [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor],UITextAttributeTextColor,nil]];

        self.tabBarController.tabBar.hidden = NO;

    }

    返回按钮

    #pragma mark - 定义导航的按钮

    - (void)navButton{

        UIButton * leftButton = [UIButton buttonWithType:UIButtonTypeCustom];

        leftButton.frame = CGRectMake(0, 0, 25, 25);

        [leftButton setImage:[UIImage imageNamed:@"back_white"] forState:0];

        [leftButton setBackgroundColor:[UIColor clearColor]];

        leftButton.titleLabel.font = [UIFont systemFontOfSize:15];

        [leftButton addTarget:self action:@selector(leftClick) forControlEvents:UIControlEventTouchUpInside];

        UIBarButtonItem * tiem = [[UIBarButtonItem alloc]initWithCustomView:leftButton];

        UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]

                                          initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace

                                          target:nil action:nil];

        negativeSpacer.width = -10;

        self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, tiem, nil];

        UIButton * rightButton = [UIButton buttonWithType:UIButtonTypeCustom];

        rightButton.frame = CGRectMake(0, 0, 25, 25);

        [rightButton setImage:[UIImage imageNamed:@"search.png"] forState:0];

        [rightButton setBackgroundColor:[UIColor clearColor]];

        rightButton.titleLabel.font = [UIFont systemFontOfSize:15];

        [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];

        UIBarButtonItem * tiemRight = [[UIBarButtonItem alloc]initWithCustomView:rightButton];

        UIBarButtonItem *negativeSpacerRight = [[UIBarButtonItem alloc]

                                                initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace

                                                target:nil action:nil];

        negativeSpacerRight.width = -10;

        self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacerRight, tiemRight, nil];

    }

    - (void)leftClick{

        [self.navigationController popViewControllerAnimated:YES];

    }

    - (void)rightButtonClick{

    }

    相关文章

      网友评论

          本文标题:导航栏的定义操作

          本文链接:https://www.haomeiwen.com/subject/jpkscftx.html