如何在IPhone中使UIBarButton正确对齐

时间:2010-07-12 05:19:39

标签: iphone

我动态添加了Toolbar,并且我动态地添加了barbutton,但它显示在左侧我想让它出现在右侧。 AnyOne可以帮助我解决这个问题吗?

 CGRect frame = CGRectMake(0, 205, 320, 40);
 UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:frame];
 toolbar.barStyle =UIBarStyleBlackTranslucent;
 toolbar.tintColor = [UIColor darkGrayColor];


 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle: @"Done" style:UIBarButtonItemStyleDone target:self action:@selector(foo)];
 NSArray *array = [[NSArray alloc]initWithObjects:backButton,nil];

 [toolbar setItems:array];

1 个答案:

答案 0 :(得分:2)

我通过简单地添加以下行获得答案

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

并在数组中添加此项 现在代码看起来像这样

self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,20,320,480)样式:UITableViewStyleGrouped];     CGRect frame = CGRectMake(0,205,320,40);     UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame:frame];     toolbar.barStyle = UIBarStyleBlackTranslucent;     toolbar.tintColor = [UIColor darkGrayColor];

UIBarButtonItem *BtnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle: @"Done" style:UIBarButtonItemStyleDone target:self action:@selector(foo)];
NSArray *array = [[NSArray alloc]initWithObjects:BtnSpace,backButton,nil];

[toolbar setItems:array];