兩種自訂系統彈出鍵盤上方的view,自訂view

來源:互聯網
上載者:User

兩種自訂系統彈出鍵盤上方的view,自訂view
        我們在很多的應用中,都可能會遇到,在彈出的鍵盤上方的view,添加一些控制項來作協助工具功能,下面我通過2種情況來介紹:

// 螢幕的物理高度#define  ScreenHeight  [UIScreen mainScreen].bounds.size.height// 螢幕的物理寬度#define  ScreenWidth   [UIScreen mainScreen].bounds.size.width@interface HMTMainViewController ()@property (nonatomic, strong) UIView *testView;@property (nonatomic, strong) UITextField *testTextField;@property (nonatomic, strong) NSNumber *duration;@property (nonatomic, strong) NSNumber *curve;@end@implementation HMTMainViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view.    self.navigationItem.title = @"Demo";        [self testCustonKeyBoardView___1];    [self testCustonKeyBoardView___2];}// 自訂系統彈出鍵盤上方的view ---> 普遍情況- (void)testCustonKeyBoardView___1{    UITextField *testTextField = [[UITextField alloc] initWithFrame:CGRectMake(60, 200, 200, 40)];    testTextField.borderStyle = UITextBorderStyleRoundedRect;    [self.view addSubview:testTextField];        // 自訂的view    UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];    customView.backgroundColor = [UIColor redColor];    testTextField.inputAccessoryView = customView;        // 往自訂view中添加各種UI控制項(以UIButton為例)    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(270, 5, 40, 30)];    button.backgroundColor = [UIColor blackColor];    [button setTitle:@"完成" forState:UIControlStateNormal];    [button addTarget:self action:@selector(didClickButtonAction) forControlEvents:UIControlEventTouchUpInside];    [customView addSubview:button];}- (void)didClickButtonAction{    NSLog(@"%s__%d__|%@",__FUNCTION__,__LINE__,@"test");    [self.view endEditing:YES];}// 自訂系統彈出鍵盤上方的view ---> ,QQ聊天等效果- (void)testCustonKeyBoardView___2{    self.testView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenHeight - 40, 320, 40)];    _testView.backgroundColor = [UIColor grayColor];    [self.view addSubview:_testView];        self.testTextField = [[UITextField alloc] initWithFrame:CGRectMake(40, 2, 200, 36)];    _testTextField.borderStyle = UITextBorderStyleRoundedRect;    [_testView addSubview:_testTextField];        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeSystem];    leftBtn.frame = CGRectMake(242, 2, 36, 36);    [leftBtn setTitle:@"-" forState:UIControlStateNormal];    leftBtn.titleLabel.font = [UIFont systemFontOfSize: 28.0];    [_testView addSubview:leftBtn];        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeSystem];    rightBtn.frame = CGRectMake(282, 2, 36, 36);    rightBtn.titleLabel.font = [UIFont systemFontOfSize: 28.0];    [rightBtn setTitle:@"+" forState:UIControlStateNormal];    [_testView addSubview:rightBtn];            // 通知-監聽鍵盤彈出事件    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];    // 通知-監聽鍵盤迴收事件    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeKeyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];}- (void)changeKeyboardWillShowNotification:(NSNotification *)notification{    NSDictionary *userInfo = [notification userInfo];    // 鍵盤彈出後的frame的結構體對象    NSValue *valueEndFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];    // 得到鍵盤彈出後的鍵盤視圖所在y座標    CGFloat keyBoardEndY = valueEndFrame.CGRectValue.origin.y;    // 鍵盤彈出的動畫時間    self.duration = [userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];    // 鍵盤彈出的動畫曲線    self.curve = [userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];        // 添加移動動畫,使視圖跟隨鍵盤移動(動畫時間和曲線都保持一致)    [UIView animateWithDuration:[_duration doubleValue] animations:^{        [UIView setAnimationBeginsFromCurrentState:YES];        // 不設定這個,大家可以看看有什麼區別        [UIView setAnimationCurve:[_curve intValue]];        _testView.center = CGPointMake(_testView.center.x, keyBoardEndY - _testView.bounds.size.height/2.0);    }];    /**     *  + (void)setAnimationCurve:(UIViewAnimationCurve)curve;     *  typedef NS_ENUM(NSInteger, UIViewAnimationCurve) {     *  UIViewAnimationCurveEaseInOut,         // 淡入淡出,開始時慢,由慢變快,中間最快,然後變慢     *  UIViewAnimationCurveEaseIn,            // 淡入,開始時慢然後越來越快     *  UIViewAnimationCurveEaseOut,           // 淡出,開始快然後越來越慢     *  UIViewAnimationCurveLinear             // 線性勻速,開始和結束是一個速度     *  };     */}- (void)changeKeyboardWillHideNotification:(NSNotification *)notification{    NSDictionary *userInfo = [notification userInfo];    // 鍵盤彈出前的frame的結構體對象    NSValue *valueStatrFrame = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];    // 得到鍵盤彈出後的鍵盤視圖所在y座標    CGFloat keyBoardStatrY = valueStatrFrame.CGRectValue.origin.y;        [UIView animateWithDuration:[_duration doubleValue] animations:^{        [UIView setAnimationBeginsFromCurrentState:YES];        [UIView setAnimationCurve:[_curve intValue]];        _testView.center = CGPointMake(_testView.center.x, keyBoardStatrY - _testView.bounds.size.height/2.0);    }];}- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    [self.view endEditing:YES];}


@樣本圖:





自訂uialertview 中輸入框怎讓鍵盤消失

將TextField的did end on exit事件串連到一個(IBAction)方法,在方法內調用textfield的resignFirstResponder方法。

-(IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
 
Webview怎預設讓編輯框得到焦點並框彈出鍵盤

雖然stringByEvaluatingJavaScriptFromString挺有用,但類似document.getElementById('myId').focus()這樣設定焦點的JS是不執行的
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.