2011-10-03 39 views
3

我已經創建了多個按鈕,但我不知道如何對齊按鈕。如何在按鈕之間創建空間?

我的代碼是在這裏:

- (void)viewDidLoad 
{ 
//[email protected]"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]; 

} 

如何創建空間B/W按鍵。請幫助我。 在此先感謝!

+0

我很好奇你爲什麼選擇不使用InterfaceBuilder來做到這一點。 – mahboudz

回答

17

您可以使用內置空間按鈕類型 UIBarButtonSystemItemFixedSpaceUIBarButtonSystemItemFlexibleSpace兩個工具欄項目之間加空格。

固定空間按鈕

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

靈活的空間按鈕

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

添加空格鍵按鈕中的其他工具欄項之間。

+0

感謝它的工作。 –

+0

@ravikumar karunanithi,不客氣! – EmptyStack

+1

這可以在UINavigationBar上完成嗎? –

相關問題