後研發的天,我終於發現這種方式來改變網頁視圖菜單控制器的箭頭方向。 首先,請在您的視圖控制器的viewDidLoad
方法中執行此操作。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow:) name:UIMenuControllerWillShowMenuNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillHide:) name:UIMenuControllerWillHideMenuNotification object:nil];
然後,添加這兩個函數,然後沿箭頭方向將始終指向上方,在這種情況下,菜單欄將始終顯示文本的下方。 (同樣的方式,你可以改變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];
}
很好的回答!奇怪的是一些邊緣情況下,這不起作用....我遇到了這樣可以正確移動箭頭的情況,iOS會以幾乎瞬間的「正確」方向重新繪製它。 – 2015-03-13 13:29:02