我知道它可能看起来非常简单并且被多次询问但是我无法让它工作。我想在我的工具栏中添加两个按钮。一个在右侧,另一个在左侧。这是代码,但是应该显示在右侧的灵活代码根本不会出现。这是代码:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(nextResult:)]];
UIBarButtonItem *done=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:@selector(showSearch:)];
[done setImage:[UIImage imageNamed:@"cancel"]];
[items addObject:done];
[toolbar setItems:items animated:NO];
答案 0 :(得分:2)
您需要使用UIBarButtonSystemItemFlexibleSpace
代替UIBarButtonSystemItemFixedSpace
代码:
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:@"cancel" style:UIBarButtonItemStylePlain target:self action:@selector(showSearch:)]];
[toolbar setItems:items animated:NO];
答案 1 :(得分:1)
系统项UIBarButtonSystemItemFixedSpace
的项目是空格,而不是按钮对象。因此它确实出现了。这只是没有按钮。
将您的代码更改为
toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStylePlain target:self action:@selector(nextResult:)]];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:space];
[items addObject:[[UIBarButtonItem alloc] initWithTitle:@"cancel" style:UIBarButtonItemStylePlain target:self action:@selector(showSearch:)]];
[toolbar setItems:items animated:NO];