iOS开发中奇技淫巧

目录


去除更改Button的标题时的闪烁动画

必须同时设置button的titleLabel.text和setTitle才行,且titleLabel.text需要放在前面

示例代码:
[Button.titleLabel setText:string];
[Button setTitle:string forState:UIControlStateNormal];

iOS7和iOS8中让tableView分割线顶头

覆盖以下两个函数即可

-(void)viewDidLayoutSubviews{
   if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
       [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
   }

   if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
   }
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
   if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
       [cell setSeparatorInset:UIEdgeInsetsZero];
   }

   if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
       [cell setLayoutMargins:UIEdgeInsetsZero];
   }
}

去掉tableView底部多余分割线

 [self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

防止键盘挡住textFiled的解决方案

需要处理键盘弹出和键盘收起两个事件

键盘出现
实现textfield的代理方法
- (void)textFieldDidBeginEditing:(UITextField *)textField{
   CGRect frame = textField.frame;
   CGPoint rootFrame = [[textField superview] convertPoint:frame.origin toView:self.view];//把控件的坐标转为相对于屏幕的坐标
   int offset = rootFrame.y + 56 - (self.view.frame.size.height - 216.0);//键盘高度216
   NSTimeInterval animationDuration = 0.30f;
   [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
   [UIView setAnimationDuration:animationDuration];
   float width = self.view.frame.size.width;
   float height = self.view.frame.size.height;
   if(offset > 0){
       CGRect rect = CGRectMake(0.0f, -offset,width,height);
       self.view.frame = rect;
   }
   [UIView commitAnimations];
}

键盘收起
- (IBAction)hideKeyboard:(id)sender {
   NSTimeInterval animationDuration = 0.30f;
   [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
   [UIView setAnimationDuration:animationDuration];
   CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
   self.view.frame = rect;
   [UIView commitAnimations];
   //在这把textField的第一焦点去掉
}

你可能感兴趣的:(ios开发)