减少UINavigationBar中栏按钮项之间的空白

时间:2014-06-13 17:48:42

标签: ios uinavigationbar uinavigationitem rightbarbuttonitem

我注意到条形按钮项之间的空白区域非常大。我想减少空间,为我的头衔留出更多空间。我试图创建固定空间然后在按钮之间添加它但它不起作用。有人知道怎么做吗?

UIBarButtonItem *fixedItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedItem.width = 10.0f;
self.navigationItem.rightBarButtonItems = @[settingsButtonItem, fixedItem, speakerButtonItem, fixedItem, favouriteButtonItem];

2 个答案:

答案 0 :(得分:1)

你关闭但你需要的是一个负面的固定空间。如果您的其他UIBarButtonItem使用自定义视图,请检查这些视图的框架。这是添加两个右边条形按钮项目的示例,它们更多地拥抱边缘。在您的情况下,您也希望在按钮之间添加负空格。

UIBarButtonItem *negativeSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpace.width = -8;

UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 44, 44);
[backButton setImage:[[UIImage imageNamed:@"ic_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
            forState:UIControlStateNormal];
backButton.imageView.tintColor = [UIColor whiteColor];
[backButton addTarget:self
               action:@selector(backPressed)
     forControlEvents:UIControlEventTouchUpInside];

self.backNavButton = [[UIBarButtonItem alloc] initWithCustomView:backButton];
self.backNavButton.enabled = NO;

UIButton *forwardButton = [UIButton buttonWithType:UIButtonTypeCustom];
forwardButton.frame = CGRectMake(0, 0, 44, 44);
[forwardButton setImage:[forwardImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]
               forState:UIControlStateNormal];
forwardButton.imageView.tintColor = [UIColor whiteColor];
[forwardButton addTarget:self
                  action:@selector(forwardPressed)
        forControlEvents:UIControlEventTouchUpInside];

self.forwardNavButton = [[UIBarButtonItem alloc] initWithCustomView:forwardButton];
self.forwardNavButton.enabled = NO;

self.navigationItem.rightBarButtonItems = @[negativeSpace,self.forwardNavButton,self.backNavButton];

答案 1 :(得分:1)

使用Xcode 6.4,这对我来说非常有效,只需一行代码!
https://stackoverflow.com/a/26469607/3634990

self.myBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 25, 0, -25);

注意 - 不幸的是,这不会移动按钮的命中区域,只移动其图像。但如果你只是需要稍微移动按钮,这是一个很好的解决方案!