7

我注意到条形按钮项目之间的空白空间非常大。我想减少空间以便为我的标题腾出更多空间。我尝试创建固定空间,然后将其添加到按钮中,但它不起作用。有人知道怎么做吗?

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

2 回答 2

1

你很近,但你需要的是一个负的固定空间。如果您的其他 UIBarButtonItems 使用自定义视图,请检查这些视图的框架。这是一个添加两个更靠近边缘的右栏按钮项目的示例。在您的情况下,您还需要在按钮之间添加负空间。

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];
于 2015-04-08T19:49:46.877 回答
1

使用 Xcode 6.4,这对我来说非常好用 1 行代码!
https://stackoverflow.com/a/26469607/3634990

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

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

于 2015-07-31T16:16:41.317 回答