简体   繁体   English

如何在UIWebView中显示之前更改UIMenuController的箭头方向

[英]how to change the arrow direction of UIMenuController just before shown in an UIWebView

I am trying to change arrow direction to be up so that when it appears it does not block a top bar I set for the application. 我正在尝试将箭头方向更改为向上,以便它出现时不会阻塞我为该应用程序设置的顶部栏。 Here is the code I used to change direction: 这是我用来更改方向的代码:

UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setArrowDirection:UIMenuControllerArrowUp];

and here is the result, honestly say, it is pretty stressful: 老实说,这就是结果,这非常令人压力: 在此处输入图片说明

It appears to be the same issue on my iphone as well. 我的iPhone上似乎也有同样的问题。

Can anyone look into this? 有人可以调查吗? Thanks 谢谢

After days of researching, I finally find this way to change arrow direction of the menu controller in webview. 经过几天的研究,我终于找到了这种方法来更改webview中菜单控制器的箭头方向。 First, please do this in your view controller's viewDidLoad method. 首先,请在您的视图控制器的viewDidLoad方法中执行此操作。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow:) name:UIMenuControllerWillShowMenuNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillHide:) name:UIMenuControllerWillHideMenuNotification object:nil];

Then, add these two functions, then the arrow direction will always be pointing up, in which case the menubar will always appear below your text. 然后,添加这两个功能,箭头方向将始终指向上方,在这种情况下,菜单栏将始终显示在文本下方。 (Same way, you can change UIMenuControllerArrowUp; to any other direction if you want your menubar to appear in other position) (以同样的方式,如果希望菜单栏出现在其他位置,则可以将UIMenuControllerArrowUp;更改为任何其他方向)

-(void)menuControllerWillShow:(NSNotification *)notification{
    //Remove Will Show Notification to prevent
    // "menuControllerWillShow" function to be called multiple times
    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIMenuControllerWillShowMenuNotification object:nil];

    //Hide the Original Menu View
    UIMenuController* menuController = [UIMenuController sharedMenuController];
    CGPoint origin = menuController.menuFrame.origin;
    CGSize size = menuController.menuFrame.size;
    CGRect menuFrame;
    menuFrame.origin = origin;
    menuFrame.size = size;
    [menuController setMenuVisible:NO animated:NO];

    //Modify its target rect and show it again to prevent glitchy appearance
    menuController.arrowDirection = UIMenuControllerArrowUp;
    [menuController setTargetRect:menuFrame inView:self.view];
    [menuController setMenuVisible:YES animated:YES];

}

-(void)menuControllerWillHide:(NSNotification *)notification{
    //re-register menuControllerWillShow
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow:) name:UIMenuControllerWillShowMenuNotification object:nil];
}

According to the Apple Doc , arrowDirection sets the direction the arrow points; 根据Apple Doc ,arrowDirection设置箭头指向的方向; it has nothing to do with the location of the menu relative to its target area. 它与菜单相对于其目标区域的位置无关。 It also looks like they don't give you any control over the positioning of the menu beyond setTargetRect:inView. 看起来他们除了setTargetRect:inView之外,对菜单的定位没有任何控制权。

If you really want to put the menu below the text, you might be able to set a "fake" target area and change the arrow direction to point to the "real" area of interest. 如果您确实希望将菜单放在文本下方,则可以设置“伪”目标区域并更改箭头方向以指向感兴趣的“真实”区域。

However, there's probably a reason Apple does it this way. 但是,苹果这样做可能是有原因的。 My guess? 我猜? If you select some text with your finger, your hand is probably obscuring the part of the screen below the text... so it's not very helpful if the menu appears there. 如果您用手指选择了一些文本,则您的手可能会遮住文本下方的屏幕部分...因此,如果菜单出现在此处并不会很有帮助。 Going out of your way to break consistency with standard UI conventions isn't usually worth the effort. 竭尽全力破坏与标准UI约定的一致性通常是不值得的。

once check this one 一旦检查这一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM