iOS 开发笔记和技巧总结 (四)

来源:互联网 发布:web安全编程 编辑:程序博客网 时间:2024/06/04 19:43
实现弹出软键盘后不遮挡文本框,自动调整屏幕高度方法

第一步:在头文件加入<UITextFieldDelegate>协议,并定义成员变量 
 
int keyBoardMargin_; //记录上一次移动的间距,用户离开文本时恢复视图高度 
 
第二步:在xib文件设置文本框的delegate与file'owner连接,或者在m文件用代码实现代理的连接(文本控件.delegate=self) 
 
int keyBoardMargin_; //记录上一次移动的间距,用户离开文本时恢复视图高度 
 
第三步:m文件中加入以下方法 
#pragma mark - 处理文本位置方法 
 
- (void)moveView:(UITextField *)textField leaveView:(BOOL)leave   
{   
    float screenHeight = 480; //屏幕尺寸,如果屏幕允许旋转,可根据旋转动态调整 
    float keyboardHeight = 216; //键盘尺寸,如果屏幕允许旋转,可根据旋转动态调整 
    float statusBarHeight,NavBarHeight,tableCellHeight,textFieldOriginY,textFieldFromButtomHeigth; 
    int margin; 
    statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height; //屏幕状态栏高度 
    NavBarHeight = self.navigationController.navigationBar.frame.size.height; //获取导航栏高度 
     
    UITableViewCell *tableViewCell=(UITableViewCell *)textField.superview; 
    tableCellHeight = tableViewCell.frame.size.height; //获取单元格高度 
 
    CGRect fieldFrame=[self.view convertRect:textField.frame fromView:tableViewCell]; 
    textFieldOriginY = fieldFrame.origin.y; //获取文本框相对本视图的y轴位置。 
     
    NSLog(@"textFieldOriginY=%f",textFieldOriginY); 
    NSLog(@"tableCellHeight=%f",tableCellHeight); 
    NSLog(@"NavBarHeight=%f",NavBarHeight); 
    NSLog(@"statusBarHeight=%f",statusBarHeight); 
     
    //计算文本框到屏幕底部的高度(屏幕高度-顶部状态栏高度-导航栏高度-文本框的的相对y轴位置-单元格高度) 
    textFieldFromButtomHeigth = screenHeight - statusBarHeight - NavBarHeight - textFieldOriginY - tableCellHeight; 
     
    if(!leave) { 
        if(textFieldFromButtomHeigth < keyboardHeight) { //如果文本框到屏幕底部的高度 < 键盘高度 
            margin = keyboardHeight - textFieldFromButtomHeigth; // 则计算差距 
            keyBoardMargin_ = margin; //keyBoardMargin_ 为成员变量,记录上一次移动的间距,用户离开文本时恢复视图高度 
        } else { 
            margin= 0; 
            keyBoardMargin_ = 0; 
        } 
    } 
    NSLog(@"keyBoardMargin_=%d",keyBoardMargin_); 
     
    c*****t float movementDuration = 0.3f; // 动画时间 
     
    int movement = (leave ? keyBoardMargin_ : -margin); //进入时根据差距移动视图,离开时恢复之前的高度 
     
    [UIView beginAnimati*****: @"textFieldAnim" context: nil]; //添加动画 
    [UIView setAnimationBeginsFromCurrentState: YES]; 
    [UIView setAnimationDuration: movementDuration]; 
    self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
    [UIView commitAnimati*****]; 
}  
 
第四步:m文件中加入以下协议方法 
 
- (void)textFieldDidBeginEditing:(UITextField *)textField   
{   
    [self moveView:textField leaveView:NO];   
}   
 
- (void)textFieldDidEndEditing:(UITextField *)textField;   
{   
    [self moveView:textField leaveView:YES];   


注:margin = keyboardHeight - textFieldFromButtomHeigth; // 则计算差距  
这里应该写 
margin = keyboardHeigh;  //也就是view向上推一个键盘的高度 
 

如果按照你写的计算差距,如果文本框刚好在键盘的高度内,会导致页面推到上面


如何将本地时间转化为"指定时区"的时


NSDateFormatter *formatter =[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSString *loctime = [formatter stringFromDate:date];

----------------------------------------------------------------------------------------------------
不管用户如何设置,,均获得24小时显示(其中的HH,强制指定为24时制)
NSDateFormatter * formatter =[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *loctime = [formatter stringFromDate:date];


0 0
原创粉丝点击