以编程方式设置 UIBarButtonItem 的位置

发布于 2024-08-30 01:54:12 字数 81 浏览 3 评论 0原文

我怎样才能设置 UIBarButtonItem 的位置?就像,我想根据状态将其设置为 UIToolbar 的最右侧或最左侧。

谢谢。

How would I be able to set the position of a UIBarButtonItem? Like, I would like to set it to be on either the very right of a UIToolbar or the very left depending on a state.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妖妓 2024-09-06 01:54:12

您不直接设置 UIBarButtonItem< 的位置/a> 在 UIToolbar 中。相反,您定义了项目的顺序,并在左侧或右侧放置了灵活的空间。

您可以做的是:

  • 创建您想要放置的 UIBarButtonItem(按钮 1)。
  • 创建一个 UIBarButtonSystemItemFlexibleSpace 类型的 UIBarButtonItem(按钮 2)。
  • 如果要将按钮放在左侧,请使用 (button 1) 和 (button 2) 创建一个数组,并使用 setItems:animated: 方法将其传递给 UIToolbar。
  • 如果要将按钮放在右侧,请使用 (button 2) 和 (button 1) 创建一个数组,并使用 setItems:animated: 方法将其传递给 UIToolbar。

You do not set directly the position of a UIBarButtonItem in an UIToolbar. Instead you defined the items' order and put flexible space on the left or on the right.

What you can do is:

  • Create the UIBarButtonItem you want to place (button 1).
  • Create an UIBarButtonItem of type UIBarButtonSystemItemFlexibleSpace (button 2).
  • If you want to put the button on the left, create an array with (button 1) and (button 2) and pass it to the UIToolbar by using the setItems:animated: method.
  • If you want to put the button on the right, create an array with (button 2) and (button 1) and pass it to the UIToolbar by using the setItems:animated: method.
南汐寒笙箫 2024-09-06 01:54:12

我希望它对您有所帮助...

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];

链接解释了Bar Button Item方法和参数
https://developer.apple.com/library..... 使用Fake Item 获取按钮上的准确捏合位置...

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];

Link explains Bar Button Item Methods and Arguments
https://developer.apple.com/library..... Use Fake Item to get exact pinch location on Button...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文