iOS: UIMenuItem menu cut-off

291 views Asked by At

I have 2 locations on my app that I use a UILongPressGestureRecognizer to show a menu with 2 choices. The two locations, Left and Right, are UILabels that display numbers. A long press on the number will popup a menu with options to either "Reset to 0" or "Cancel".

I get the menu to popup without a problem. However, I've run into a problem after the menus are displayed: the display of RIGHT-SIDE menu items is cutoff, almost in half. I've included an image to help illustrate my problem (parts of the image are intentionally pixelated). The menu item works, it calls the correct method, it just doesn't display correctly. I have a call to -(BOOL)canBecomeFirstResponder {return YES;} later in the class.

Here's my code for the left-side menu:

- (IBAction)leftActionLongPress:(UILongPressGestureRecognizer*)recognizer
{
// On a long press, show popup menu with selections to reset the number to
// zero or not

[self.leftActionNameNumber canBecomeFirstResponder];

// Check if the number is not a zero
if ([self.leftActionNameNumber.text isEqualToString:@"0"]) {
    // Equal to zero so don't show the popup menu
    return;
} else {
    // Number is not a zero, show popup menu
    UIMenuItem* resetMenu =
        [[UIMenuItem alloc] initWithTitle:@"Reset to 0"
                                   action:@selector(resetLeftToZero)];
    UIMenuItem* cancelMenu =
        [[UIMenuItem alloc] initWithTitle:@"Cancel"
                                   action:@selector(leaveNumberAsIs)];

    UIMenuController* menu = [UIMenuController sharedMenuController];
    [menu setMenuItems:[NSArray arrayWithObjects:resetMenu, cancelMenu, nil]];
    [menu setTargetRect:self.leftActionNameNumber.frame inView:self.view];
    [menu setMenuVisible:YES animated:YES];
}

Here's the code for the right-side menu:

- (IBAction)rightActionLongPress:(UILongPressGestureRecognizer*)recognizer
{
// On a long press, show popup menu with selections to reset the number to
// zero or not

[self.rightActionNameNumber canBecomeFirstResponder];


// Check if the number is not a zero
if ([self.rightActionNameNumber.text isEqualToString:@"0"]) {
    // Equal to zero so don't show the popup menu
    return;
} else {
    // Number is not a zero, show popup menu
    UIMenuItem* resetMenu =
        [[UIMenuItem alloc] initWithTitle:@"Reset to 0"
                                   action:@selector(resetRightToZero)];
    UIMenuItem* cancelMenu =
        [[UIMenuItem alloc] initWithTitle:@"Cancel"
                                   action:@selector(leaveNumberAsIs)];

    UIMenuController* menu = [UIMenuController sharedMenuController];
    [menu setMenuItems:[NSArray arrayWithObjects:resetMenu, cancelMenu, nil]];
    [menu setTargetRect:self.rightActionNameNumber.frame inView:self.view];
    [menu setMenuVisible:YES animated:YES];

}

Image displaying UIMenuItem with cut-off menu

0

There are 0 answers