您的位置:首页 > 移动开发 > Objective-C

Objective-C-如何自定义键盘(iOS)

2016-09-21 13:25 218 查看
//createMyInputView该方法返回一个UIImageView类型的视图

    UIImageView *myView= [self
createMyInputView];

   
//让此视图作为键盘的背景视图

    textField.inputView = myView;

    

    //添加附件区域

    UIView *upView=[[UIView
alloc]initWithFrame:CGRectMake(0,
0, self.view.frame.size.width,
60)];

   
//附件区域的颜色

    upView.backgroundColor=[UIColor
grayColor];

    //让upView作为附件区域

    textField.inputAccessoryView = upView;

    

   
//在附件区域中添加“确定”按钮

    UIButton *sendButton=[UIButton
buttonWithType:UIButtonTypeSystem];

    sendButton.frame=CGRectMake(self.view.frame.size.width-80,
6, 80,
40);

    [sendButton setTitle:@"确定"
forState:UIControlStateNormal];

    [upView addSubview:sendButton];

//自定义键盘

-(UIImageView *)createMyInputView{

    

    //创建一个背景ImageView

    UIImageView *inputView=[[UIImageView
alloc]initWithFrame:CGRectMake(0,
0, self.view.frame.size.width,
200)];

    //添加背景图片

    inputView.image = [UIImage
imageNamed:@"DOVE 1"];

    //打开用户交互

    inputView.userInteractionEnabled =
YES;

    //视图的背景色

    inputView.backgroundColor=[UIColor
colorWithRed:1
green:1
blue:1
alpha:0.5];

   

    //添加按钮

    NSArray *titleArray=@[@"京",@"津",@"追",@"梦",@"人",@"自",@"定",@"义",@"键",@"盘",@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十",@"取钱",@"红包",@"收"];

    

   
//有多少个字就创建多少个按钮

    for(int i =
0; i< titleArray.count; i++)

    {

        //创建按钮

        UIButton *button=[UIButton
buttonWithType:UIButtonTypeSystem];

        //按钮的坐标

        button.frame=CGRectMake(40*(i%10),
i/10*40,
40, 40);

        //按钮上显示的文字

        [button setTitle:titleArray[i]
forState:UIControlStateNormal];

        //把创建的每一个按钮添加到inputView上

        [inputView addSubview:button];

        //设置按钮的tag值

        button.tag = i+1;

       
//给每个按钮添加点击事件

        [button addTarget:self
action:@selector(btnClick:)
forControlEvents:UIControlEventTouchUpInside];

    }

    //返回背景视图

    return inputView;

}

//按钮的点击事件

-(void)btnClick:(UIButton *)button

{

   
//在此处做响应的处理

    

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息