首页 > 代码库 > iOS--导航栏样式

iOS--导航栏样式

push返回按钮样式:

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = item;
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    [self.navigationItem setHidesBackButton:YES];

自由点击事件:

//用户协议跳转
    UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(labelClick)];
    
    // 2. 将点击事件添加到label上
    [self.UserRate addGestureRecognizer:labelTapGestureRecognizer];
    self.UserRate.userInteractionEnabled = YES; // 可以理解为设置label可被点击

创建左右导航栏按钮,搜索与个人中心:

//创建左右导航栏按钮,搜索与个人中心
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[self scaleToSize:[UIImage imageNamed:@"sousuo.png"] size:CGSizeMake(20, 19.6)] style:UIBarButtonItemStylePlain target:self action:@selector(Actionleft:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[self scaleToSize:[UIImage imageNamed:@"persons.png"] size:CGSizeMake(20, 20)] style:UIBarButtonItemStylePlain target:self action:@selector(Actionright:)];



//导航栏个人信息按钮
-(void)Actionright:(id)send{
    NSLog(@"跳转到个人中心页面");
    PersonalViewController *PersonalView = [self.storyboard instantiateViewControllerWithIdentifier:@"PersonalView"];
    [self.navigationController pushViewController:PersonalView animated:PersonalView];
}
//导航栏搜索按钮点击事件
-(void)Actionleft:(id)send{
    NSLog(@"跳转到搜索页面");
    SearchViewController *search = [self.storyboard instantiateViewControllerWithIdentifier:@"search"];
    [self.navigationController pushViewController:search animated:search];
    
}
//设置图片大小
- (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
    // 创建一个bitmap的context
    // 并把它设置成为当前正在使用的context
    UIGraphicsBeginImageContext(size);
     // 绘制改变大小的图片
    [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
     // 从当前context中创建一个改变大小后的图片
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
   // 使当前的context出堆栈
    UIGraphicsEndImageContext();
   // 返回新的改变大小后的图片
    return scaledImage;
}

 

iOS--导航栏样式