以编程方式设置UIBarButtonItem的位置

4

我该如何设置UIBarButtonItem的位置?比如,我想根据状态将其设置在UIToolbar的最右侧或最左侧。

谢谢。

2个回答

18
您不能直接设置UIBarButtonItemUIToolbar中的位置。相反,您需要定义项目的顺序并在左侧或右侧放置可伸缩空间。
您可以执行以下操作:
  • 创建要放置的UIBarButtonItem(按钮1)。
  • 创建类型为UIBarButtonSystemItemFlexibleSpace的UIBarButtonItem(按钮2)。
  • 如果要将按钮放置在左侧,请创建一个包含(按钮1)和(按钮2)的数组,并使用setItems:animated:方法将其传递给UIToolbar。
  • 如果要将按钮放置在右侧,请创建一个包含(按钮2)和(按钮1)的数组,并使用setItems:animated:方法将其传递给UIToolbar。

3

I hope it helps you...

UIToolbar* keyboardDoneButtonView = [[UIToolbar alloc] init];
[keyboardDoneButtonView sizeToFit];

UIBarButtonItem* PrevButton = [[UIBarButtonItem alloc]  initWithBarButtonSystemItem:105 target:nil  action:nil]; //<
UIBarButtonItem*  NextButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:106  target:nil action:nil]; //>
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(doneClicked:)];

UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc]  initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *fake = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil] ;

[keyboardDoneButtonView setItems:[NSArray arrayWithObjects: PrevButton,fake, NextButton,fake,flexSpace,fake,doneButton,nil] animated:YES];

链接解释了工具栏按钮项的方法和参数 https://developer.apple.com/library..... 使用项目可以精确获取按键的捏合位置...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接