码迷,mamicode.com
首页 > 其他好文 > 详细

调整导航条上leftBarButtonItem和rightBarButtonItem与屏幕边界的间距

时间:2015-04-25 18:28:53      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:bug

  我们发现,在设置navigationItem的leftBarButtonItem或rightBarButtonItem时,用CustomView初始化UIBarButtonItem,不论怎么设置CustomView的frame,添加到导航条上之后总是和屏幕边界有一定的间距(5pix),如何自由调整这个间距呢?


初始化一个用于控制间距的UIBarButtonItem实例negativeSpacer,并设置negativeSpacer的width属性的值,设为-5的时候,正好可以使按钮与屏幕边界值为0,以rightBarButtonItem情况为例 

[plain] view plaincopy



  1. UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  2. /**  
  3.  *  设置frame只能控制按钮的大小  
  4.  */  
  5. btn.frame= CGRectMake(0, 0, 40, 44);  
  6. [btn addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];  
  7. UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:btn];  
  8. UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   
  9.                                    initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   
  10.                                    target:nil action:nil];  
  11. /**  
  12.  *  width为负数时,相当于btn向右移动width数值个像素,由于按钮本身和边界间距为5pix,所以width设为-5时,间距正好调整  
  13.  *  为0;width为正数时,正好相反,相当于往左移动width数值个像素  
  14.  */  
  15. negativeSpacer.width = -5;   
  16. self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];  
  17. [btn_right release];  

再看一个例子:

    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];

    btnBack.frame = CGRectMake(0, 0, 23, 23);

    btnBack.backgroundColor = [UIColor clearColor];

    [btnBack setImage:[UIImage imageNamed:@"bar_back_nomal.png"] forState:UIControlStateNormal];

    [btnBack setImage:[UIImage imageNamed:@"bar_back_selected.png"] forState:UIControlStateHighlighted];

    [btnBack setImage:[UIImage imageNamed:@"bar_back_selected.png"] forState:UIControlStateSelected];

    [btnBack addTarget:self action:@selector(btnBackClicked:) forControlEvents:UIControlEventTouchUpInside];

    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc]initWithCustomView:btnBack]];

    UIButton * source = [UIButton buttonWithType:UIButtonTypeCustom];

    source.frame = CGRectMake(0, 0, 35, 35);

    source.backgroundColor = [UIColor clearColor];

    [source setImage:[UIImage imageNamed:@"head_normal.png"] forState:UIControlStateNormal];

    [source setImage:[UIImage imageNamed:@"head_press.png"] forState:UIControlStateHighlighted];

    [source setImage:[UIImage imageNamed:@"head_press.png"] forState:UIControlStateSelected];

    [source addTarget:self action:@selector(btnPublicNum:) forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem * btn_right = [[UIBarButtonItem alloc]initWithCustomView:source];

    UIBarButtonItem * negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

    negativeSpacer.width = -6;

    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];

OK:左右边界一模一样




调整导航条上leftBarButtonItem和rightBarButtonItem与屏幕边界的间距

标签:bug

原文地址:http://blog.csdn.net/u011862058/article/details/45271025

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!