Add spacing between UIToolbar

2019-04-03 08:49发布

I have a toolbar that looks like the following:

enter image description here

The issue is that it is kind of cluttered and therefore I would like to add some spacing to it. I tried doing:

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

But it still gives me the same thing. How can I add a 10px between these UIBarButtonItems?

3条回答
我只想做你的唯一
2楼-- · 2019-04-03 09:26
UIBarButtonItem *fixedSpace = 
  [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
                                                target:nil 
                                                action:nil];
fixedSpace.width = 10;
查看更多
混吃等死
3楼-- · 2019-04-03 09:30

I use this code to generate UIBarButtonItems, it's some header file which I #import if needed.

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];
}
查看更多
Animai°情兽
4楼-- · 2019-04-03 09:34

You need to add space in between the items what u r looking for. this can be done by..

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

hope this will help u.

查看更多
登录 后发表回答