【问题标题】:Can't adjust spacing of UIBarButtonItem(s) when assigned to navigationItem.rightBarButtonItems分配给 navigationItem.rightBarButtonItems 时无法调整 UIBarButtonItem(s) 的间距
【发布时间】:2014-08-28 07:51:40
【问题描述】:

这是一个奇怪的问题。

情况是我想调整 UIBarButtonItem(s) 之间的间距,使它们仅相隔 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 的工作方式似乎与所有其他工具栏不同。

【问题讨论】:

    标签: iphone ios ipad


    【解决方案1】:

    将所有 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;
    

    【讨论】:

    • 这确实有效,而且是我之前所做的。我想要做的是将间距调整为 2 像素,而无需创建新的 UIToolbar。看来我应该能够使用 self.navigationItem.rightBarButtonItems 并放置在正确的间隔对象中。此方法允许我添加所有正确的组件,但是当我将项目添加到导航栏时它不起作用。其他工具栏工作正常。有什么想法吗?
    • 我真的找不到更好的方法,所以我将您的答案标记为正确答案。感谢您的帮助。
    【解决方案2】:

    最简单的方法是创建一个方法来为每个 UIBarButtonItem 设置 UIButton,如下所示:

    - (UIBarButtonItem*)createAddAlbumBarButtonWithImage:(NSString*)imageName
    {
        UIButton* button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
        [button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(addAlbum:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem* barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    
        return barButton;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多