2011-07-23 9 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]; 

しかし、それでも私は同じことを与えます。これらの間に10xを追加するにはどうすればいいですか?UIBarButtonItems

答えて

43
UIBarButtonItem *fixedSpace = 
    [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
               target:nil 
               action:nil]; 
fixedSpace.width = 10; 
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]; 
} 
1

あなたが探しているものの間にスペースを追加する必要があります。 これは次の方法で行うことができます。

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

希望これはあなたに役立ちます。