问题
I have a toolbar that looks like the following:
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
?
回答1:
UIBarButtonItem *fixedSpace =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
fixedSpace.width = 10;
回答2:
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.
回答3:
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];
}
来源:https://stackoverflow.com/questions/6798573/add-spacing-between-uitoolbar