【问题标题】:Add spacing between UIToolbar在 UIToolbar 之间添加间距
【发布时间】:2011-07-23 06:03:18
【问题描述】:

我有一个如下所示的工具栏:

问题是它有点杂乱,因此我想给它添加一些间距。我试着做:

UIBarButtonItem *spacer = 
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                               target:nil 
                                               action:nil];

self.toolbar_array = 
 [[NSMutableArray alloc] initWithObjects:self.mention, 
                                         spacer, 
                                         self.picture, 
                                         spacer, 
                                         share, 
                                         spacer, 
                                         self.message, nil];

但它仍然给了我同样的东西。如何在这些 UIBarButtonItems 之间添加 10px?

【问题讨论】:

    标签: objective-c iphone ipad uibarbuttonitem toolbaritems


    【解决方案1】:
    UIBarButtonItem *fixedSpace = 
      [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
                                                    target:nil 
                                                    action:nil];
    fixedSpace.width = 10;
    

    【讨论】:

      【解决方案2】:

      您需要在您要查找的项目之间添加空格。 这可以通过..

      UIBarButtonItem *fixedSpace = 
       [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
                                                     target:nil 
                                                     action:nil];
      fixedSpace.width = 10;
      

      希望这对你有帮助。

      【讨论】:

        【解决方案3】:

        我使用此代码生成 UIBarButtonItems,它是一些头文件,如果需要,我会 #import。

        static inline UIBarButtonItem *BarButtonWithText(NSString *text, 
                                                         id target, 
                                                         SEL action) {
            NSString *localizedText = NSLocalizedString(text, nil);
            return [[[UIBarButtonItem alloc] initWithTitle:localizedText 
                                                     style:UIBarButtonItemStyleBordered 
                                                    target:target 
                                                    action:action] autorelease];
        }
        
        static inline UIBarButtonItem *BarButtonWithImage(NSString *imageName, 
                                                          id target, 
                                                          SEL action) {
            UIImage *image = [UIImage imageNamed:imageName];
            return [[[UIBarButtonItem alloc] initWithImage:image 
                                                     style:UIBarButtonItemStylePlain 
                                                    target:target 
                                                    action:action] autorelease];
        }
        
        static inline UIBarButtonItem *BarButtonWithSystemStyle(UIBarButtonSystemItem style, 
                              id target, 
                              SEL action) {
            return [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:style 
                                                                  target:target 
                                                                  action:action] autorelease];
        }
        
        static inline UIBarButtonItem *BarButtonWithFlexibleWidth(id target, SEL action) {
            return [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                                                  target:target 
                                                                  action:action] autorelease];
        }
        
        static inline UIBarButtonItem *BarButtonWithFixedWidth(CGFloat width, 
                                                               id target, 
                                                               SEL action) {
            UIBarButtonItem *button = 
              [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
                                                            target:target 
                                                            action:action];
            button.width = width;
            return [button autorelease];
        }
        

        【讨论】: