2011-07-23 88 views
19

之間的間距我有一個工具欄,如下所示:添加UIToolbar

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?

回答

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

我使用這段代碼來生成UIBarButtonItems,它是一些我需要的時候導入的頭文件。

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

您需要在項目之間添加所需的空間。 這可以通過..

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

希望做這將幫助ü。