2

我的应用程序上有 2 个位置,我使用 UILongPressGestureRecognizer 显示具有 2 个选项的菜单。Left 和 Right 这两个位置是显示数字的 UILabel。长按数字将弹出一个菜单,其中包含“重置为 0”或“取消”选项。

我可以毫无问题地弹出菜单。但是,我在显示菜单后遇到了一个问题:RIGHT-SIDE 菜单项的显示被截断,几乎一半。我已经包含了一张图片来帮助说明我的问题(部分图片是故意像素化的)。菜单项有效,它调用了正确的方法,只是无法正确显示。我-(BOOL)canBecomeFirstResponder {return YES;}在课后有一个电话。

这是我的左侧菜单代码:

- (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];
}

这是右侧菜单的代码:

- (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];

}

显示带有截止菜单的 UIMenuItem 的图像

4

0 回答 0