iPhone, is menuCont.arrowDirection = UIMenuControl

2019-09-11 03:24发布

问题:

I'm trying to add a menu controller as a quick choice of options after tapping my tab bar, however I don't get the arrow, when I use

menuCont.arrowDirection = UIMenuControllerArrowDown;

But if I use Up, Left or Right it works fine.

Can someone else try this and advise ?

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [[event allTouches] anyObject];

if ([touch tapCount] == 2) {
    [self becomeFirstResponder];
    UIMenuItem *menuItem = [[UIMenuItem alloc] initWithTitle:@"Blue" action:@selector(blueView)];
    UIMenuItem *menuItem2 = [[UIMenuItem alloc] initWithTitle:@"Green" action:@selector(greenView)];
    UIMenuItem *menuItem3 = [[UIMenuItem alloc] initWithTitle:@"Red" action:@selector(redView)];

    UIMenuController *menuCont = [UIMenuController sharedMenuController];
    [menuCont setTargetRect:CGRectMake(0,0,320,200) inView:self.view];
    menuCont.menuItems = [NSArray arrayWithObjects:menuItem, menuItem2, menuItem3, nil];
    menuCont.arrowDirection = UIMenuControllerArrowDown;
    [menuCont setMenuVisible:YES animated:YES];
}
}

- (void)blueView {
self.view.backgroundColor = [UIColor blueColor];
}
- (void)greenView {
self.view.backgroundColor = [UIColor greenColor];
}
- (void)redView {
self.view.backgroundColor = [UIColor redColor];
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
BOOL answer = NO;

if (action == @selector(blueView)) 
    answer = YES;

if (action == @selector(greenView)) 
    answer = YES;

if (action == @selector(redView)) 
    answer = YES;

return answer;
}

- (BOOL)canBecomeFirstResponder {
return YES;
}

PS. I'm wondering if its old school, as it doesn't auto-complete when entering some of the menu controller code ?

回答1:

see the Documentation of the UIMenuController.

setTargetRect:inView:
Sets the area in a view above or below which the editing menu is positioned.

which means, if you specify a rect starting at x=0 y=0, and you want a downArrow, the program tries to draw the Menu above your rect. And this is not possible because UIMenus are always in the visible area of the screen.

use [menuCont setTargetRect:CGRectMake(0,100,320,200) inView:self.view]; and see the down-arrow



回答2:

It seems the bottom of the UIMenuController is being cut-off. Try offsetting the location:

[menuCont setTargetRect:CGRectMake(0,-20,320,200) inView:self.view];