gpt4 book ai didi

iphone - 如何显示 UIMenuController 到 UIBarButtonItem

转载 作者:太空狗 更新时间:2023-10-30 03:58:01 25 4
gpt4 key购买 nike

如何在 UIBarButtonItem 下显示 UIMenuController 点击它?

最佳答案

假设您的 UIBarButtonItem 已连接到:

-(void)buttonClicked:(UIBarButtonItem*)sender event:(UIEvent*)event;

然后将这些代码粘贴到您的 View Controller 中:

-(void)buttonClicked:(UIBarButtonItem*)sender event:(UIEvent*)event{
[self becomeFirstResponder];

/*get the view from the UIBarButtonItem*/
UIView *buttonView=[[event.allTouches anyObject] view];
CGRect buttonFrame=[buttonView convertRect:buttonView.frame toView:self.view];

UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *resetMenuItem = [[UIMenuItem alloc] initWithTitle:@"Menu Item" action:@selector(menuItemClicked:)];

NSAssert([self becomeFirstResponder], @"Sorry, UIMenuController will not work with %@ since it cannot become first responder", self);
[menuController setMenuItems:[NSArray arrayWithObject:resetMenuItem]];
[menuController setTargetRect:buttonFrame inView:self.view];
[menuController setMenuVisible:YES animated:YES];

[resetMenuItem release];
}
- (void) copy:(id) sender {
// called when copy clicked in menu
}
- (void) menuItemClicked:(id) sender {
// called when Item clicked in menu
}
- (BOOL) canPerformAction:(SEL)selector withSender:(id) sender {
if (selector == @selector(menuItemClicked:) /*|| selector == @selector(copy:)*/ /*<--enable that if you want the copy item */) {
return YES;
}
return NO;
}
- (BOOL) canBecomeFirstResponder {
return YES;
}

关键是对 canBecomeFirstResponder 和 canPerformAction 返回 YES。

Here是示例项目,如果您需要的话。

这些代码实际上来自stackoverflow的其他帖子,我只是将它们组合在一起。

Figure out UIBarButtonItem frame in window?

How to get UIMenuController work for a custom view?

关于iphone - 如何显示 UIMenuController 到 UIBarButtonItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5405238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com