2016-03-10 75 views
2

當我選擇WebView內容時如何刪除默認菜單項,如複製,過去,全選。如何將自定義操作置於默認菜單項目的中間。我將這些內容顯示在最後,我想從我的自定義操作開始。 我在下面的代碼中使用didAppear方法。如何顯示自iOS開始的自定義菜單項?

UIMenuItem *customMenuItem1=[[UIMenuItem alloc] initWithTitle:@"Highlight" action:@selector(customAction1:)]; 
UIMenuItem *customMenuItem2=[[UIMenuItem alloc] initWithTitle:@"UnHighlight" action:@selector(UnHighlighted:)]; 
[[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:customMenuItem1,customMenuItem2,nil]]; 
[UIMenuController sharedMenuController].menuVisible=YES; 

請幫幫我。

+0

你試試這個http://stackoverflow.com/questions/2955354/showing-custom-menu-on-selection-in-uiwebview-in-iphone –

+0

是,我試過這個 –

+0

我試過了,它可以工作,但它增加了菜單項到現有的。它不會刪除這些。 –

回答

0

enter image description here如這裏回答showing custom menu on selection in UIWebView in iphone

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.google.com"]]]; 
    self.webview.backgroundColor = [UIColor blueColor]; 
    // Do any additional setup after loading the view. 
} 

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 

    UIMenuItem *customMenuItem1 = [[UIMenuItem alloc] initWithTitle:@"Custom 1" action:@selector(customAction1:)]; 
    UIMenuItem *customMenuItem2 = [[UIMenuItem alloc] initWithTitle:@"Custom 2" action:@selector(customAction2:)]; 
    [[UIMenuController sharedMenuController] setMenuItems:[NSArray arrayWithObjects:customMenuItem1, customMenuItem2, nil]]; 

} 

- (void)viewDidDisappear:(BOOL)animated 
{ 
    [super viewDidDisappear:animated]; 
    [[UIMenuController sharedMenuController] setMenuItems:nil]; 
} 
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{ 
    if (self.webview.superview != nil)// && ![urlTextField isFirstResponder]) 
    { 
     if (action == @selector(customAction1:) || action == @selector(customAction2:)) 
     { 
      return YES; 
     } 
    } 
    return [super canPerformAction:action withSender:sender]; 
} 
-(void)customAction1:(UIMenuItem*)item 
{} 
-(void)customAction2:(UIMenuItem*)item 
{} 
+0

如何交換副本以突出顯示 –