zoukankan      html  css  js  c++  java
  • iOS8 自定义navigationbar 以及 UIBarButtonItem 边距问题

    一。自定义navigationbar

    - (void)initNavigationBar{
    
        [self.navigationController setNavigationBarHidden:YES];
    
        UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 32)];
    
        [bar setBackgroundImage:[UIImage imageNamed:@"zhuche_bar2.png"] forBarMetrics:UIBarMetricsDefault]; 
    
        UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:nil];
    
        UIButton *left = [UIButton buttonWithType:UIButtonTypeCustom];
    
        [left setFrame:CGRectMake(0, 2, 28, 28)];
    
        [left setImage:[UIImage imageNamed:@"zhuche_back.png"] forState:UIControlStateNormal];
    
        [left addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:left];
    
        [item setLeftBarButtonItem:leftButton];
    
        [bar pushNavigationItem:item animated:NO];
    
        [self.view addSubview:bar];
    
    }
    
    
    - (void)back{
    
        [self.navigationController popViewControllerAnimated:YES];
    
    }

    虽然可以解决自定义navigationbar的问题,左右按钮都可以替换,但是如果放在UITableViewController中,自定义的navigationbar会随着scrollview的滑动而滑动,不会像系统自带的navigationbar一样始终保持在屏幕最上方。那么我们可以有第二种解决方式,修改原来的navigationbar,其实就是改一下leftBarButtonItem或者rightBarButtonItem在navigationbar上面的位置:修改UIBarButtonItem 的边距

    二。修改UIBarButtonItem 的边距

    @interface UINavigationItem (margin)
    
    @end
    
    @implementation UINavigationItem (margin)
    
    #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_6_1
    - (void)setLeftBarButtonItem:(UIBarButtonItem *)_leftBarButtonItem
    {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
        {
            UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSeperator.width = -16;//此处修改到边界的距离,请自行测试
            
            if (_leftBarButtonItem)
            {
                [self setLeftBarButtonItems:@[negativeSeperator, _leftBarButtonItem]];
            }
            else
            {
                [self setLeftBarButtonItems:@[negativeSeperator]];
            }
           
            
        }
        else
        {
            [self setLeftBarButtonItem:_leftBarButtonItem animated:NO];
        }
    }
    
    - (void)setRightBarButtonItem:(UIBarButtonItem *)_rightBarButtonItem
    {
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
        {
            UIBarButtonItem *negativeSeperator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
            negativeSeperator.width = -12;//此处修改到边界的距离,请自行测试
            
            if (_rightBarButtonItem)
            {
                [self setRightBarButtonItems:@[negativeSeperator, _rightBarButtonItem]];
            }
            else
            {
                [self setRightBarButtonItems:@[negativeSeperator]];
            }
            
        }
        else
        {
            [self setRightBarButtonItem:_rightBarButtonItem animated:NO];
        }
    }
    
    #endif
    @end

    以上代码可以放置在主视图控制器的.m文件里面,@implementation ViewController的上面

    参考资料:http://blog.csdn.net/reylen/article/details/16338781;

    http://blog.163.com/lhl_soft/blog/static/201750004201292043750125/;

  • 相关阅读:
    springmvc @ResponseBody返回json 报406 not acceptable
    Java连接mysql中遇到的一些问题及解决方法
    nginx + keepalive 实现高可用
    nginx 内置变量
    nginx 防盗链
    nginx 跨域设置
    nginx 跨域设置
    nginx 日志分割
    servlet 下载地址 jcp
    图片压缩 jdk 1.8兼容问题
  • 原文地址:https://www.cnblogs.com/6duxz/p/4030916.html
Copyright © 2011-2022 走看看