UIPopoverController 出现在错误的位置
所以我花了一些时间寻找这个问题的答案,但到目前为止还没有找到任何东西。
我试图通过 UIInputAccessoryView 上的按钮呈现一个弹出窗口。我想要显示弹出窗口的 UIBarButtonItem 有一个自定义视图,因此我可以使用图像。我这样创建按钮:
buttonImage=[UIImage imageNamed:@"tags.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* compButton2=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(tagIt:) forControlEvents:UIControlEventTouchUpInside];
当需要显示弹出窗口时,我这样做:
CGRect tRect=[((UIButton*)sender) convertRect:((UIButton*)sender).frame toView:self.notePlainText];
NSLog(@"TagIt tRect: %f %f %f %f", tRect.origin.x, tRect.origin.y, tRect.size.width, tRect.size.height);
[self.popoverController presentPopoverFromRect:tRect inView:self.notePlainText permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
但我得到的是这样的:
弹出窗口看起来不错,但它出现在第二个按钮上,而它应该出现在第一个按钮上。
然后我发现了这个问题:UIBarButtonItem with custom image and no border我想,“啊,如果我从 barbuttonitem 中呈现弹出窗口而不是这样做,也许效果会更好。代码子类 UIBarButtonItem 是为了拥有一个带有图像而不是文本的按钮,并表示调用操作时正确地使用 barbuttonitem。这使我可以使用 ...presentPopoverFromBarButtonItem...
因此,我尝试了上述问题的第二个答案中的代码,并得到了更好的结果,弹出窗口指向按钮。如我所愿,但如果设备的方向不是纵向且按钮位于底部,则弹出窗口会显示错误的方向:
最后,我要指出的是,我在程序的其他地方还有其他不在 UIInputAccessoryViews 中的栏按钮项,它们可以正确显示且没有问题。它似乎只发生在输入附件视图中。
这只是:隐藏和显示键盘似乎使这项工作变得更好一些。查看按钮的帧 (CGRect) 数据,它在旋转之间不会发生变化,除非它被隐藏并恢复。这是一个错误吗?
所以..我的问题是:从嵌入 ui 附件视图中的栏按钮项呈现弹出窗口的首选、最佳实践方式是什么?
非常感谢。
So I've spent some time looking for an answer for this, but so far haven't found anything.
I'm trying to present a popover from a button on an UIInputAccessoryView. The UIBarButtonItem i want to display the popover from has a custom view so I can use an image. I create the button like this:
buttonImage=[UIImage imageNamed:@"tags.png"];
aButton=[UIButton buttonWithType:UIButtonTypeCustom];
[aButton setImage:buttonImage forState:UIControlStateNormal];
aButton.frame=CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
UIBarButtonItem* compButton2=[[UIBarButtonItem alloc]initWithCustomView:aButton];
[aButton addTarget:self action:@selector(tagIt:) forControlEvents:UIControlEventTouchUpInside];
When it comes time to display the popover, I do it like this:
CGRect tRect=[((UIButton*)sender) convertRect:((UIButton*)sender).frame toView:self.notePlainText];
NSLog(@"TagIt tRect: %f %f %f %f", tRect.origin.x, tRect.origin.y, tRect.size.width, tRect.size.height);
[self.popoverController presentPopoverFromRect:tRect inView:self.notePlainText permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
But what I get is this:
The popover looks fine, but it appears over the second button when it should be appearing over the first.
So then I found this question: UIBarButtonItem with custom image and no border and I thought, "ah, perhaps it will work better if I present the popover from a barbuttonitem instead of doing it this way. The code sub classes UIBarButtonItem in order to have a button with an image, not text, and represents the barbuttonitem properly when the action is called. This allowed me to use ...presentPopoverFromBarButtonItem...
So I tried the code in the second answer in the aforementioned question, and got a result that was better. The popover points right to the button as I wanted, but if the orientation of the device is not portrait with the button on the bottom, the popover displays with the wrong orientation:
Finally, I will point out that I have other bar button items elsewhere in the program that are not in UIInputAccessoryViews that come up properly and without issue. It only seems to be happening form the input accessory view.
This just in: Hiding and showing the keyboard seems to make this work a bit better. Looking at the frame (CGRect) data for the button, it doesn't change between rotations unless it's hidden and brought back. Is this a bug?
So.. my question is this: what is the preferred, best-practices way of presenting a popover from a bar button item embedded in a ui accessory view?
Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
所以我让它与下面的笨拙代码一起工作,尽管如果有人能告诉我为什么我必须这样做(或者为什么我不应该这样做),我将非常感激。
So I got this to work with the following kludgy code, although if someone could tell me why I have to do this (or why I SHOULDNT do this) I would be much obliged.
我遇到了完全相同的问题,弹出窗口指向 iOS 7 中的不同位置,感谢 Chris 的回答 我是终于能够看到 iOS 6 和 iOS 7 之间的一致性。我必须对代码进行 1 个小调整,将
frame
替换为bounds
,以使其定位在我期望的位置是。I had the exact same issue where the popover was pointing at a different place in iOS 7 and thanks to Chris' answer I was finally able to see consistency between iOS 6 and iOS 7. I had to make 1 minor adjustment to the code by replacing
frame
withbounds
to have it positioned where I expected it to be.使用presentPopoverFromBarButtonItem:permissedArrowDirections:animated。为此,请确保您拥有对栏按钮的引用(通过将其存储在视图控制器上的属性中,或使用 IBOutlet),然后在要显示的弹出窗口上调用该方法。
Use
presentPopoverFromBarButtonItem:permittedArrowDirections:animated
. To do this, make sure you have a reference to your bar button (either by storing it in a property on the view controller, or using an IBOutlet) and then call the method on the popover you want to show.我在这个答案中使用了相同的解决方法;这似乎是 iOS 4.3(可能是 4.x)的问题。它似乎在 iOS 5 中得到了修复。
I used the same workaround in this answer; this appears to be an issue with iOS 4.3 (maybe 4.x). It appears to be fixed in iOS 5.
试试这个:
Try This one:
您可以抵消该点:
You can just offset that point: