在UIToolbar之间添加间距

时间:2011-07-23 06:03:19

标签: objective-c iphone ipad uibarbuttonitem toolbaritems

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

enter image description here

问题是它有点混乱,因此我想为它添加一些间距。我试过了:

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?

3 个答案:

答案 0 :(得分:46)

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

答案 1 :(得分: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];
}

答案 2 :(得分:2)

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

UIBarButtonItem *fixedSpace = 
 [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
                                               target:nil 
                                               action:nil];
fixedSpace.width = 10;
希望这会对你有所帮助。