美文网首页
iOS 设置导航按钮的默认偏移量问题

iOS 设置导航按钮的默认偏移量问题

作者: XieHenry | 来源:发表于2017-01-04 15:22 被阅读388次

以前导航的做按钮或者右按钮一直没注意一些细节问题,比如下图:

pic1.png

但是新出的图是这样的:

pic2.png

右边完全是没空隙的,紧贴着,而且图片变大,点击的也变大
因此,查了一下网上的方法,在此记录一下:

左边的方法为:
    UIButton *backButton =[UIButton  buttonWithType:UIButtonTypeCustom];
    [backButton setBackgroundImage:[UIImage imageNamed:imageNamed] forState:UIControlStateNormal];
    backButton.frame=CGRectMake(0, 0, 44, 44);
    [backButton addTarget:self action:@selector(leftAction) forControlEvents:(UIControlEventTouchUpInside)];
    UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithCustomView:backButton];
    UIBarButtonItem *navSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    navSpace.width = - 15;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:navSpace,item, nil];
右边的方法为:
    UIButton *addsearchForTalentButton =[UIButton  buttonWithType:UIButtonTypeCustom];
    [addsearchForTalentButton setBackgroundImage:[UIImage imageNamed:@"search_talents_icon_search"] forState:UIControlStateNormal];
    addsearchForTalentButton.frame=CGRectMake(0, 0, 44, 44);
    [addsearchForTalentButton addTarget:self action:@selector(addsearchForTalentButtonClick) forControlEvents:(UIControlEventTouchUpInside)];   
    UIBarButtonItem*item=[[UIBarButtonItem alloc]initWithCustomView:addsearchForTalentButton];
    UIBarButtonItem *navSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    navSpace.width = - 15;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:navSpace,item, nil];

最后实现的结果如图所示:

pic3.png

相关文章

网友评论

      本文标题:iOS 设置导航按钮的默认偏移量问题

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