gpt4 book ai didi

iphone - 分配给 navigationItem.rightBarButtonItems 时无法调整 UIBarButtonItem 的间距

转载 作者:行者123 更新时间:2023-12-03 19:51:53 24 4
gpt4 key购买 nike

这是一个奇怪的问题。

情况是我想调整 UIBarButtonItem 之间的间距,使它们仅相距 2 个像素。

我可以使用 UIToolbar 轻松完成此操作:

 // Make bottom button bar buttons
NSMutableArray *bottomButtons = [[NSMutableArray alloc] initWithCapacity:3];

// Create spacer between buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[bottomButtons addObject:spacer];
UIBarButtonItem* noSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
noSpace.width = -10;


// Add button 1
self.addAlbumButton = [UIGlossyButton buttonWithType:UIButtonTypeCustom];
[self.addAlbumButton setTitle:@"Button 1" forState:UIControlStateNormal];
[self.addAlbumButton addTarget:self action:@selector(addAlbum:) forControlEvents:UIControlEventTouchUpInside];
self.addAlbumBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.addAlbumButton];
[bottomButtons addObject:self.addAlbumBarButton];

[bottomButtons addObject:noSpace];

// Add button2
self.downloadAllItemsButton = [UIGlossyButton buttonWithType:UIButtonTypeCustom];
[self.downloadAllItemsButton setTitle:@"Button 2" forState:UIControlStateNormal];
[self.downloadAllItemsButton addTarget:self action:@selector(downloadAllItemsAction:) forControlEvents:UIControlEventTouchUpInside];
self.downloadAllItemsBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.downloadAllItemsButton];
[bottomButtons addObject:self.downloadAllItemsBarButton];

// add all button to bottom toolbar
[self.bottomToolbar setItems:bottomButtons];

问题是当我尝试使用导航栏执行此操作时。由于某种原因,当我插入固定长度按钮(具有负值)时,它不会缩小按钮之间的空间。我知道固定长度按钮在那里并且可以工作,因为如果我将宽度更改为正数,按钮之间的间距就会增加。

代码基本相同,只是我没有将按钮添加到 self.bottomToolbar,而是调用以下代码:

 self.navigationItem.rightBarButtonItems = bottomButtons;

我在 MasterViewController 中发现了同样的问题。我正在使用 splitviewcontroller,底部工具栏工作正常,但顶部工具栏有相同的间距问题。这个问题是我无法使按钮之间的间距小于默认值。

似乎 navigationItem.rightBarButtonItems 的工作方式与所有其他工具栏不同。

最佳答案

将所有 navigationButtonItem 添加到数组后,您可以按照以下方式执行此操作。我想这也会对你有帮助,因为这对我有用,如果你想改变按钮之间的空间,你可以在宏的帮助下改变它

#define ONE_BUTTON_WIDTH 30.0f
#define SPACE_BETWEEN_BUTTONS 12.0f
#define ONE_BUTTON_TOTAL_WIDTH (ONE_BUTTON_WIDTH + SPACE_BETWEEN_BUTTONS)
#define kBookmarksImage [UIImage imageNamed:@"bookmarks.png"]

/* CREATE BOOKMARKS BUTTON */
UIButton *bookmarksButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, kBookmarksImage.size.width, kBookmarksImage.size.height)];
[bookmarksButton setImage:kBookmarksImage forState:UIControlStateNormal];
[bookmarksButton addTarget:target action:@selector(toolbarButtonTapped:) forControlEvents:UIControlEventTouchDown];
bookmarksButton.tag = kBookmarksButtonTag;

UIBarButtonItem *bookmarksButtonItem = [[UIBarButtonItem alloc] initWithCustomView:bookmarksButton];

UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
spacer.width = SPACE_BETWEEN_BUTTONS;
[buttons addObjectsFromArray:@[bookmarksButtonItem,spacer]];


/* ADD ALL THESE BUTTONS TO CUSTOM TOOLBAR AND TOOLBAR TO NAVIGATION BAR */
UIToolbar *customToolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, ([bottomButtons count]/2*ONE_BUTTON_TOTAL_WIDTH), 44.01f)]; // 44.01 shifts it up 1px for some reason

customToolbar.clearsContextBeforeDrawing = NO;
customToolbar.clipsToBounds = NO;
customToolbar.tintColor = [UIColor colorWithWhite:0.305f alpha:0.0f]; // closest I could get by eye to black, translucent style.
customToolbar.barStyle = -1; // clear background
[customToolbar setItems: bottomButtons animated:NO];

UIBarButtonItem *customUIBarButtonitem = [[UIBarButtonItem alloc] initWithCustomView:customToolbar];
self.navigationItem.rightBarButtonItem = customUIBarButtonitem;

关于iphone - 分配给 navigationItem.rightBarButtonItems 时无法调整 UIBarButtonItem 的间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18879948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com