20

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

在此处输入图像描述

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

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];

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

4

3 回答 3

48
UIBarButtonItem *fixedSpace = 
  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
                                                target:nil 
                                                action:nil];
fixedSpace.width = 10;
于 2011-07-23T06:13:59.780 回答
3

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

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

希望这会帮助你。

于 2011-07-23T06:45:15.427 回答
2

我使用此代码生成 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];
}
于 2011-07-23T06:44:30.357 回答