2011-11-21 87 views
1

我在UIBarButtonItem中只有一個按鈕和一個圖像。我想將它放在屏幕中間,但無法調整框架。請參閱代碼,任何線索都會有用。UIBarButtonItem中的按鈕框架

- (void)viewDidLoad { 
//self.navigationItem.hidesBackButton = NO; 

[super viewDidLoad]; 



UIImage *emailImage = [UIImage imageNamed:@"Email_Button.png"]; 
UIButton *emailButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
emailButton.frame = CGRectMake(0, 0, 100, 30); 
    [emailButton setImage:emailImage forState:UIControlStateNormal]; 



UIBarButtonItem *aBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:emailButton]; 
//[aBarButtonItem setFrame:CGRectMake(100, 0, 100, 30)]; 

[emailButton addTarget:self action:@selector(sendEmail) forControlEvents:UIControlEventTouchUpInside]; 

NSMutableArray * arr = [NSMutableArray arrayWithObjects:aBarButtonItem, nil]; 
[[self navigationController] setToolbarHidden: NO animated:YES]; 
[self setToolbarItems:arr animated:YES]; 
} 

謝謝

回答

4

如果你想在中間,你應該使用兩個靈活的空間欄按鈕項目,一個按鈕,就可把一個在你的UIBarButtonItem的每一面,應該有效果你的願望......

創建一個像這樣

UIBarButtonItem *flexible = [[UIBarButtonItem alloc] `initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]` 

希望它可以幫助

+0

完美工作。謝謝。 – curious123

0
for (UIView *view in self.navigationController.navigationBar.subviews) { 
    if ([NSStringFromClass([view class]) isEqualToString:@"UINavigationButton"]) { 
     [view setFrame:CGRectMake(0, 0, 50, 30)]; 
     [self.navigationController.navigationBar addSubview:view]; 
    } 
}