键盘弹出-PickerView

原创 2016年06月02日 09:42:02

这里写图片描述

1.ViewController.m

//
//  ViewController.m
//

#import "ViewController.h"
#import "SimulateActionSheet.h"

@interface ViewController ()<SimulateActionSheetDelegate, UIPickerViewDataSource>{
    SimulateActionSheet *sheet;
    NSArray* arrays;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    arrays = [NSArray arrayWithObjects:@"中国",@"美国",@"英国",@"日本",@"法国", nil];
}

- (IBAction)action:(id)sender {

    sheet = [SimulateActionSheet styleDefault];
    sheet.delegate = self;

    //必须在设置delegate之后调用,否则无法选中指定的行
    [sheet selectRow:arrays.count/2 inComponent:0 animated:YES];
    [sheet show:self];
}

-(void)actionCancle{
    [sheet dismiss:self];
}

-(void)actionDone{
    [sheet dismiss:self];

    NSUInteger index = [sheet selectedRowInComponent:0];

    NSLog(@"done with index of %lu",(unsigned long)index);
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return arrays.count;
}

-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [arrays objectAtIndex:row];
}

@end

2.SimulateActionSheet.h

//
//  SimulateActionSheet.h
//
#import <UIKit/UIKit.h>

@protocol SimulateActionSheetDelegate <UIPickerViewDelegate>
//点击取消的回调接口
-(void)actionCancle;
//点击确定的回调接口
-(void)actionDone;

@end

@interface SimulateActionSheet : UIView

@property(assign, nonatomic) id<SimulateActionSheetDelegate> delegate;
@property(strong, nonatomic)UIView* toolBar;
@property(strong, nonatomic)UIPickerView* pickerView;

+(instancetype)styleDefault;
-(void)show:(UIViewController *)controller;
-(void)dismiss:(UIViewController *)controller;
//选中指定的行列
-(void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)anime;
//获取被选中的行列
-(NSInteger)selectedRowInComponent:(NSInteger)component;

@end

3.SimulateActionSheet.m

//
//  SimulateActionSheet.m
//

#import "SimulateActionSheet.h"
#define RGBACOLOR(r,g,b,a) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:(a)]

@interface SimulateActionSheet(){
    UIColor *toolBarColor;
    UIColor *textColorNormal;
    UIColor *textColorPressed;
    UIColor *pickerBgColor;
}
@end

@implementation SimulateActionSheet
+(instancetype)styleDefault{
    SimulateActionSheet* sheet = [[SimulateActionSheet alloc]initWithFrame:CGRectMake(
                                                                                     0,
                                                                                     0,
                                                                                     UIScreen.mainScreen.bounds.size.width,
                                                                                     UIScreen.mainScreen.bounds.size.height)];

    [sheet setBackgroundColor:[UIColor clearColor]];
    sheet.toolBar = [sheet actionToolBar];
    sheet.pickerView = [sheet actionPicker];
    [sheet addSubview:sheet.toolBar];
    [sheet addSubview:sheet.pickerView];

    return sheet;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];

    if (self != nil) {
        toolBarColor = RGBACOLOR(240.0, 240.0, 240.0, 0.9);
        textColorNormal = RGBACOLOR(0, 146.0, 255.0, 1);
        textColorPressed = RGBACOLOR(209.0, 213.0, 219.0, 0.9);
        pickerBgColor = textColorPressed;
    }

    return self;
}
-(void)setupInitPostion:(UIViewController *)controller{
    [UIApplication.sharedApplication.keyWindow?UIApplication.sharedApplication.keyWindow:UIApplication.sharedApplication.windows[0]
                                                                              addSubview:self];
    [self.superview bringSubviewToFront:self];
    CGFloat pickerViewYpositionHidden = UIScreen.mainScreen.bounds.size.height;
    [self.pickerView setFrame:CGRectMake(self.pickerView.frame.origin.x,
                                         pickerViewYpositionHidden,
                                         self.pickerView.frame.size.width,
                                         self.pickerView.frame.size.height)];
    [self.toolBar setFrame:CGRectMake(self.toolBar.frame.origin.x,
                                      pickerViewYpositionHidden,
                                      self.toolBar.frame.size.width,
                                      self.toolBar.frame.size.height)];
}
-(void)show:(UIViewController *)controller{
    [self setupInitPostion:controller];

    CGFloat toolBarYposition = UIScreen.mainScreen.bounds.size.height -
    (self.pickerView.frame.size.height + self.toolBar.frame.size.height);

    [UIView animateWithDuration:0.25f
                     animations:^{
                         [self setBackgroundColor:[[UIColor darkGrayColor] colorWithAlphaComponent:0.5]];
                         [controller.view setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];
                         [controller.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeDimmed];

                         [self.toolBar setFrame:CGRectMake(self.toolBar.frame.origin.x,
                                                            toolBarYposition,
                                                            self.toolBar.frame.size.width,
                                                            self.toolBar.frame.size.height)];

                         [self.pickerView setFrame:CGRectMake(self.pickerView.frame.origin.x,
                                                     toolBarYposition+self.toolBar.frame.size.height,
                                                     self.pickerView.frame.size.width,
                                                     self.pickerView.frame.size.height)];
                     }
                     completion:nil];

}
-(void)dismiss:(UIViewController *)controller{
    [UIView animateWithDuration:0.25f
                     animations:^{
                         [self setBackgroundColor:[UIColor clearColor]];
                         [controller.view setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
                         [controller.navigationController.navigationBar setTintAdjustmentMode:UIViewTintAdjustmentModeNormal];
                         [self.subviews enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                             UIView* v = (UIView*)obj;
                             [v setFrame:CGRectMake(v.frame.origin.x,
                                                    UIScreen.mainScreen.bounds.size.height,
                                                    v.frame.size.width,
                                                    v.frame.size.height)];
                         }];
                     }
                     completion:^(BOOL finished) {
                         [self removeFromSuperview];
                     }];

}

-(UIView *)actionToolBar{
    UIView *tools = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width, 44)];
    tools.backgroundColor = toolBarColor;
    UIButton *cancle = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    [cancle setTitle:@"取消" forState:UIControlStateNormal];
    [cancle setTitleColor:textColorPressed forState:UIControlStateHighlighted];
    [cancle setTitleColor:textColorNormal forState:UIControlStateNormal];
    [cancle addTarget:self action:@selector(actionCancle) forControlEvents:UIControlEventTouchUpInside];
    [cancle sizeToFit];
    [tools addSubview:cancle];

    cancle.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *cancleConstraintLeft = [NSLayoutConstraint constraintWithItem:cancle attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeLeading multiplier:1.0f constant:10.0f];
    NSLayoutConstraint *cancleConstrainY = [NSLayoutConstraint constraintWithItem:cancle attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0];
    [tools addConstraint:cancleConstraintLeft];
    [tools addConstraint:cancleConstrainY];

    UIButton *ok = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    [ok setTitle:@"确定" forState:UIControlStateNormal];
    [ok setTitleColor:textColorNormal forState:UIControlStateNormal];
    [ok setTitleColor:textColorPressed forState:UIControlStateHighlighted];
    [ok addTarget:self action:@selector(actionDone) forControlEvents:UIControlEventTouchUpInside];
    [ok sizeToFit];
    [tools addSubview:ok];

    ok.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *okConstraintRight = [NSLayoutConstraint constraintWithItem:ok attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:-10.0f];
    NSLayoutConstraint *okConstraintY = [NSLayoutConstraint constraintWithItem:ok attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:tools attribute:NSLayoutAttributeCenterY multiplier:1.0f constant:0];
    [tools addConstraint:okConstraintRight];
    [tools addConstraint:okConstraintY];

    return tools;
}

-(UIPickerView *)actionPicker;{
    UIPickerView *picker = [[UIPickerView alloc]initWithFrame:CGRectMake(self.pickerView.frame.origin.x,
                                                                         self.bounds.size.height,
                                                                         self.bounds.size.width,
                                                                         216)];
    picker.showsSelectionIndicator=YES;
    [picker setBackgroundColor:pickerBgColor];

    return picker;
}

-(void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)anime{
    [_pickerView selectRow:row inComponent:component animated:anime];
}

-(NSInteger)selectedRowInComponent:(NSInteger)component{
    return [_pickerView selectedRowInComponent:component];
}

-(void)actionDone{
    if([_delegate respondsToSelector:@selector(actionDone)]){
        [_delegate actionDone];
    }
}

-(void)actionCancle{
    if ([_delegate respondsToSelector:@selector(actionCancle)]) {
        [_delegate actionCancle];
    }
}
-(void)setDelegate:(id<SimulateActionSheetDelegate>)delegate{
    _delegate = delegate;
    _pickerView.delegate = delegate;
}
@end

版权声明:本文为博主原创文章,未经博主允许不得转载。

点击textField弹出自定义pickerView的完整解决方案

这几天做一个应用有这样一个需求,有一个'新建收货地址'的页面(tableView),其中有一个textField是输入城市的,要求是点击textField(输入框)后,下面弹出来一个自定义的带有too...
  • qfeung
  • qfeung
  • 2016年09月12日 17:15
  • 3040

Android中自定义可以选择中文的NumberPicker屏蔽弹出软键盘

自定义NumberPicer可以显示中文选择时,会自动弹出软键盘。上网查找了很多方法,说什么EditText焦点去除之类的,发现都没有用,不过在下面这个链接里面找到了答案: http://blog....
  • BuptZhengChaoJie
  • BuptZhengChaoJie
  • 2015年05月17日 14:09
  • 1840

Swift - 选择框(UIPickerView)的用法

import UIKitclass ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { ...
  • github_26672553
  • github_26672553
  • 2016年06月25日 15:19
  • 2093

ios 自定义弹出UIPickerView或UIDatePicker(动画效果)

前面iOS学习之UIPickerView控件的简单使用 用到的UIPickerView弹出来是通过 textField.inputView = selectPicker;   textField....
  • qilixiang012
  • qilixiang012
  • 2014年11月21日 00:03
  • 828

iOS学习笔记01-自定义简单弹出pickerView

UIPickerView平常用的地方好像也不是很多,顶多就是一些需要选择的地方,这次项目需要这一个功能,我就单独写了一个简单的demo,效果图如下:新增主页面弹出view,在主页面添加的代码有个小问题...
  • sinat_31511831
  • sinat_31511831
  • 2017年01月12日 14:47
  • 396

UIPickerView组件的使用之国旗联动——使用XIB自定义组件

具体步骤如下: 1 2 3 4 5 6 7 8 9 10 11
  • u013087513
  • u013087513
  • 2016年01月03日 23:17
  • 628

OCUI界面设计:UIPickerView 拾取视图

简介UIPickerView 是一个选择器控件, 它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活。 UIPickerView 直接继承了 UIVi...
  • Hierarch_Lee
  • Hierarch_Lee
  • 2016年01月19日 21:24
  • 1975

UITextField通过UIPickerView显示选择内容

通过UIPickerView 选中内容在textField中编辑性别
  • amberMengling
  • amberMengling
  • 2015年09月23日 14:52
  • 1457

IOS之UIPickerView添加确定,取消按钮

txtfield点击出现pickerview if (txtField.tag == 301) { [txtField resignFirstResponder]; ...
  • LVXIANGAN
  • LVXIANGAN
  • 2015年06月03日 08:33
  • 12418

iOS - UIPickerView文字大小颜色修改,无限轮播,无限循环滚动

最近项目里用到了UIPickerView,要求无限轮播,简单看了看UIPickView的所有属性和所有代理方法,没有无限轮播的设置,遂去百度了一把,发现网上流传着一个帖子,大家各种转载,看了看后,明白...
  • icefishlily
  • icefishlily
  • 2016年09月29日 10:31
  • 2967
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:键盘弹出-PickerView
举报原因:
原因补充:

(最多只允许输入30个字)