如何在 iOS8 自定义键盘中制作类似键盘字符的弹出窗口?

18

我想在iOS8的自定义键盘中创建如下图所示的弹出窗口。

要在iOS8中创建这种类型的键盘

有一些代码可以工作,但无法访问键盘的外层窗口,导致出现如下图2所示的问题。

iOS8自定义键盘中的问题

2个回答

6

我在我的自定义键盘中做了这个,它可以正常工作。

//adding pop up when character is tapped
- (void)addPopupToButton:(UIButton *)button
{


    CGRect frame,frame1;
    if(self.view.frame.size.width == 320)
    {
        //Keyboard is in Portrait
        frame = CGRectMake(0, -25, 28, 43);
        frame1=CGRectMake(0, 0, 28, 43);

    }
    else{
        //Keyboard is in Landscape
        frame = CGRectMake(3, -25, 35, 43);
        frame1=CGRectMake(0, 10, 35, 43);

    }
   //create pop up view
    UIView *popUp=[[UIView alloc]initWithFrame:frame];

    //create a label to add to pop up view
    UILabel *text = [[UILabel alloc] init];

    //set frame for the label and set label title
    [text setFrame:frame1];
    [text setText:button.titleLabel.text];
    text.textAlignment=NSTextAlignmentCenter;
    [text setFont:[UIFont boldSystemFontOfSize:30]];
    text.backgroundColor=[UIColor whiteColor];

    //add label as popup view's subview
    [popUp addSubview:text];

    //add pop up view as button's subview
    [button addSubview:popUp];

}


//remove Pop up view
-(void)endPopUpForButton:(UIButton*)button
{
    if ([button subviews].count > 1)
    {
        [[[button subviews] objectAtIndex:1] removeFromSuperview];
    }
}

enter image description here


1
你好,CodeIgniter能否在320*216框架之外添加弹出窗口?因为我想要一个更大的弹出窗口,它超出了这个框架,但问题是视图的外部部分被裁剪掉了,如上图所示。 - Scorpian Alive
2
@ScorpianAlive,我也遇到了同样的问题!目前我已将其设置为320.0。这只是临时的解决方案,因为我将要为一个更大的弹出窗口工作。 - codeIgnitor
1
@codeIgniter,我在询问能否将键盘高度调整为大于216? - Scorpian Alive
@codeIgnitor 谢谢,你的链接对于自定义高度很有用。但是,如何处理纵向和横向方向的高度变化呢?因为在横向方向上保持相同的高度,我尝试应用相同的代码来减小高度,但无法处理键盘高度的方向变化。 - Vijay Hirpara
@VijayHirpara,你的意思是你尝试在willAnimateRotationToInterfaceOrientation方法中运行代码,结果如何? - codeIgnitor

4
根据Apple App Extension Programming guide的说明,无法在自定义键盘的主视图上方显示关键艺术品,就像在iPhone上点击并按住顶部行中的键时系统键盘所做的那样。因此,似乎不能在键盘框架外添加弹出窗口,因此codelgnitor的答案是您可以做的最好的事情。

我们可以编写自定义代码来实现相同的功能吗?我发现弹出窗口的代码只能显示单个字符,虽然它可以正常工作,但我需要为某些键显示多个字符。 - Vijay Hirpara
您可以在键盘上放置一个隐藏的键盘按键行,然后当用户对某个键进行长按手势时将其显示出来。 当用户按下该键盘行上的任何键时,再次隐藏它。 - Mx83

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接