iOS - UIToolBar 作为 UITextView 的 inputAccessoryView

发布于 2025-01-03 08:52:50 字数 684 浏览 4 评论 0原文

我添加了一个带有 UIBarButtonItemUIToolBar 作为 UITextView 的 inputAccessoryView。它工作正常,但 UIBarButtonItem 在其框架之外是可触摸的,也许在右侧外侧 50 像素处。这没什么大不了的,但它让我很烦恼。有人知道为什么吗?

这是我的代码(ARC):

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneWriting:)];
[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];

self.messageTextView.inputAccessoryView = toolBar;

I've added a UIToolBar with a UIBarButtonItem as inputAccessoryView for a UITextView. It works fine but the UIBarButtonItem is touchable outside it's frame, perhaps 50 pixels outside to the right. It's no big deal but it annoys me. Anyone know why?

This is my code (ARC):

UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height, 320, 44)];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneWriting:)];
[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];

self.messageTextView.inputAccessoryView = toolBar;

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

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

发布评论

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

评论(3

゛清羽墨安 2025-01-10 08:52:50

在 iOS 6 中,它的表现似乎符合预期。
不错的提示:如果您希望按钮显示在右侧而不是左侧,请使用以下选项之一:

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

然后使用以下命令初始化工具栏:

[toolBar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]];

In iOS 6 it seems to behave as expected.
Nice tip: If you want the button to appear on the right instead of the left, use one of these:

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

Then initialise the toolbar with:

[toolBar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, nil]];
并安 2025-01-10 08:52:50

如果工具栏中没有其他附近的按钮,工具栏似乎会将按钮的活动区域扩展到其范围之外。苹果工程师肯定认为,最好尝试猜测用户想要按的位置,而不是根本不做出反应。

The toolbar seems to expand the active area of the buttons beyond their bounds if there are no other nearby buttons in the toolbar. Apple engineers must think it is better to try to guess where the user intended to press rather than not react at all.

灼痛 2025-01-10 08:52:50

我希望它可以帮助您...

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

使用项目来获取按钮上的精确捏合位置...

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

Use Fake Item to get exact pinch location on Button...

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