首页 技术 正文
技术 2022年11月14日
0 收藏 472 点赞 2,229 浏览 1656 个字

  UIPickerView是开发中常用的控件,日期选择、年龄选择、城市的多级联动等等都会使用,它一般是在点击某个按钮后出现,展现方式和UITextView一样,从页面底部弹出,选中后或者点击控件以外区域自动缩回。

系统原生的picker view是不支持自动弹出收回的,所以我们要对它进行一下改造。

思路:为了模仿键盘的弹出收回效果,我们设置一个UITextView,点击它就能吊起键盘。UITextView有一个inputview,我们只要将其替换成自己需要的picker view即可。

  效果图

自动弹出pickerview

主要代码:新建一个View继承与UIView,定义两个视图textView合pickerView。然后创建他们,如下:

- (void)createContentView {
self.textView = [[EXNoPasteTextField alloc] initWithFrame:self.bounds];
self.textView.font = self.textFont;
self.textView.autocorrectionType = UITextAutocorrectionTypeNo;
[self addSubview:self.textView]; _pickerView = [[UIPickerView alloc] init];
self.pickerView.dataSource = self;
self.pickerView.delegate = self;
self.textView.inputView = _pickerView; UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.size.width, )];
toolBar.barStyle = UIBarStyleDefault; UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTouched:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelTouched:)]; // the middle button is to make the Done button align to right
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], doneButton, nil]];
self.textView.inputAccessoryView = toolBar;
}

手机端是完全没问题的,iPad端会展示联想和复制按钮,需要自定义一个继承UITextView的TextView,屏蔽其粘贴功能。

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menu = [UIMenuController sharedMenuController];
if (menu) {
menu.menuVisible = NO;
} return NO;
}

  

需要源码的,可以去我的GitHub:https://github.com/zhanghua0926/EXPickerTextView

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,036
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,522
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,370
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,151
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,784
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,866