如何在按钮之间创建空间?

时间:2011-10-03 09:14:59

标签: iphone

我已经创建了多个按钮,但我不知道如何对齐按钮。

我的代码在这里:

- (void)viewDidLoad 
{
//self.title=@"Asset Management";
[super viewDidLoad];

listOfItems = [[NSMutableArray alloc] init];

[listOfItems addObject:@"User Information"];
[listOfItems addObject:@"Regional Settings"];
[listOfItems addObject:@"Configuration"];

toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 20, 320, 44)];
//UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGs
toolbar.tintColor = [UIColor clearColor];
[toolbar setTranslucent:YES];

// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];

// Create button1
UIBarButtonItem *propertiesButton = [[UIBarButtonItem alloc]
                    initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(button1Pressed)];
[buttons addObject:propertiesButton];
[propertiesButton release];

// Create button2
UIBarButtonItem *commentaryButton = [[UIBarButtonItem alloc]
                    initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(button2Pressed)];
[buttons addObject:commentaryButton];
[commentaryButton release];

// Create button3
UIBarButtonItem *versionsButton = [[UIBarButtonItem alloc]
                  initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(button3Pressed)];
[buttons addObject:versionsButton];
[versionsButton release];

// stick the buttons in the toolbar
[toolbar setItems:buttons animated:NO];
//self.toolbarItems = buttons;
[buttons release];

// and put the toolbar in the nav bar
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease]];
[toolbar release];

}

如何通过按钮创建空间。请帮助我。 提前致谢!

1 个答案:

答案 0 :(得分:17)

您可以使用两种内置空间按钮类型之一在工具栏项之间添加空格 UIBarButtonSystemItemFixedSpace UIBarButtonSystemItemFlexibleSpace

固定空间按钮

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] 
                   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace 
                                        target:nil 
                                        action:nil];
[fixedSpace setWidth:20];

灵活空间按钮

UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] 
                initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace 
                                     target:nil 
                                     action:nil];

在其他工具栏项之间添加空格键。