如何在键盘上添加两个工具栏

时间:2017-01-17 12:35:11

标签: ios objective-c textview uitoolbar

我可以在键盘上添加一个工具栏,但我不知道如何通过键盘为单个textView添加两个连续的工具栏。

我可以像下面一样添加一个工具栏

enter image description here

但我想做的就像下面的附件一样。

enter image description here

2 个答案:

答案 0 :(得分:0)

没有内置的方法可以做到这一点。您需要自己推出UIView / UIToolbar并将其放在键盘上方UIToolBar - 您可以使用系统通知收听UIKeyboard事件并调整第二个{{1}相应的框架属性。

答案 1 :(得分:-1)

您可以使用UIToolbar并自定义它以将按钮添加到其中。你可以添加这样的按钮。

  -(void)addToolBarOnKeyBordOnTextField:(UITextView *)textview
{
    if (!viewToolbar) {
        viewToolbar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 110)];
        [viewToolbar setBackgroundColor:[UIColor clearColor]];

        UIToolbar * numberToolbar1 = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width, 50)];
        numberToolbar1.barStyle = UIBarStyleBlack;
        numberToolbar1.translucent = YES;

        UIButton *buttonTopLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 50)];
        [buttonTopLeft setTitle:@"Clear" forState:UIControlStateNormal];
        [numberToolbar1 addSubview:buttonTopLeft];

        UIButton *buttonTopRight = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 0, 60, 50)];
        [buttonTopRight setTitle:@"Done" forState:UIControlStateNormal];
        [numberToolbar1 addSubview:buttonTopRight];

        [viewToolbar addSubview:numberToolbar1];

        UIToolbar *numberToolbar2 = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 60,self.view.frame.size.width, 50)];
        numberToolbar2.barStyle = UIBarStyleBlack;
        numberToolbar2.translucent = YES;

        UIButton *buttonBottomLeft = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 50)];
        [buttonBottomLeft setTitle:@"Clear" forState:UIControlStateNormal];
        [numberToolbar2 addSubview:buttonBottomLeft];

        UIButton *buttonBottomRight = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 60, 0, 60, 50)];
        [buttonBottomRight setTitle:@"Done" forState:UIControlStateNormal];
        [numberToolbar2 addSubview:buttonBottomRight];

        [viewToolbar addSubview:numberToolbar2];


    }
    [textview setInputAccessoryView:viewToolbar];

}

请记得在textViewShouldBeginEditing中调用此方法。您可以添加其余按钮并按照代码中的设置设置帧。