第一種方式(CGAffineTransform):
通過CGAffineTransformMakeTranslation方法來臨時改變位置,然后通過CGAffineTransformIdentity恢復位置;
如果需要加動畫,直接放在UIView的animation的block里就可以了。
// 比如這樣用 [UIView animateWithDuration:MJRefreshFastAnimationDuration animations:^{ self.arrowView.transform = CGAffineTransformIdentity; }];
第二種方式(UITableView):
1.初始化及添加通知觀察者 - (void)viewDidLoad { [super viewDidLoad]; self.tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; //鍵盤將要顯示時候的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boardWillShow:) name:UIKeyboardWillShowNotification object:nil]; //鍵盤將要結束時候的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(boardDidHide:) name:UIKeyboardDidHideNotification object:nil]; } 2.實現通知的響應方法 - (void)boardWillShow:(NSNotification *)sender{ //獲得鍵盤的尺寸 CGRect keyBoardRect=[sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; //當鍵盤將要顯示時,將tableView的下邊距增跟改為鍵盤的高度 self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0); } - (void)boardDidHide:(NSNotification *)sender{ //當鍵盤將要消失時,邊距還原初始狀態 self.tableView.contentInset = UIEdgeInsetsZero; }
//測試 - (BOOL)textFieldShouldReturn:(UITextField *)textField{ //取消當前輸入框的第一響應者 [textField resignFirstResponder]; return YES; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return 15; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *ider = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ider]; if (!cell) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ider]; } UITextField *TF = [[UITextField alloc] initWithFrame:CGRectMake(100, 0, 150, 44)]; TF.placeholder = @"請輸入"; TF.delegate =self; //文本框添加代理 [cell.contentView addSubview:TF]; cell.textLabel.text = @"測試"; return cell; }
轉載請注明出處:http://ficow.cn