iOS开发小问题总结

2016-10-20  本文已影响20人  yuwei66

在开发过程中,我们避免不了一些小问题的出现,现在为了方便之后的查找,将这些小问题进行持续总结更新。那我们开始总结吧。

在iOS7.0之前,我们修改的方法是
UIColor * color = [UIColor whiteColor];    
NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:UITextAttributeTextColor];    
 self.navigationBar.titleTextAttributes = dict;
在iOS7.0之后,我们修改的方法是
UIColor * color = [UIColor whiteColor];    
NSDictionary * dict=[NSDictionary dictionaryWithObject:color forKey:NSForegroundColorAttributeName];    
 self.navigationBar.titleTextAttributes = dict;
    UIWebView *callWebView = [[UIWebView alloc] init];
    NSURL *telURL = [NSURL URLWithString:@"tel:10086"];
    [callWebView loadRequest:[NSURLRequest requestWithURL:telURL]];
    [self.view addSubview:callWebView];
 //监听键盘
    //官方定义好的UIKeyboardWillShowNotification 通知名称
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//两个方法(当键盘弹起、当键盘收回)
-(void)KeyboardWillShow:(NSNotification *)sender;
- (void)KeyboardWillHide:(NSNotification *)sender;
// 打开相机
- (void)openCamera {
    // UIImagePickerControllerCameraDeviceRear 后置摄像头
    // UIImagePickerControllerCameraDeviceFront 前置摄像头
    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    if (!isCamera) {
        NSLog(@"没有摄像头");
        return ;
    }
     
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self;
    // 编辑模式
    imagePicker.allowsEditing = YES;
     
    [self  presentViewController:imagePicker animated:YES completion:^{
    }];
 
     
}
 
 
// 打开相册
- (void)openPics {
 
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.delegate = self;
    [self  presentViewController:imagePicker animated:YES completion:^{
    }];
     
     
}
 
 
// 选中照片
 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSLog(@"%@", info);
    UIImageView  *imageView = (UIImageView *)[self.view viewWithTag:101];
    // UIImagePickerControllerOriginalImage 原始图片
    // UIImagePickerControllerEditedImage 编辑后图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    imageView.image = image;
    [picker dismissViewControllerAnimated:YES completion:NULL];
     
}
 
 
 
// 取消相册
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
 
}
将general里面的Device Orientation里的后三个选项去掉。

调整代码如下:
    //监听键盘
    //官方定义好的UIKeyboardWillShowNotification 通知名称
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];


- (void)KeyboardWillShow:(NSNotification *)sender {
   
    //4s的屏幕
    if (HEIGHT==480) {
        
        if ( login_phoneField.editing==YES) {
            CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
            CGFloat  height = rect.size.height;
            NSLog(@"%f",height/HEIGHT);
            
            //    UIKeyboardAnimationDurationUserInfoKey 获取键盘升起动画时间
            //[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
            //获取动画时间
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
            self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/6);
            [UIView commitAnimations];//开始执行动画
        }else if(login_passWordField.editing==YES){
            CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
            CGFloat  height = rect.size.height;
            NSLog(@"%f",height);
            
            //    UIKeyboardAnimationDurationUserInfoKey 获取键盘升起动画时间
            //[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
            //获取动画时间
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
            self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/6);
            [UIView commitAnimations];//开始执行动画
            
        }

    }else if (HEIGHT==568){
        //5的屏幕
        if ( login_phoneField.editing==YES) {
            CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
            CGFloat  height = rect.size.height;
            NSLog(@"%f",height/HEIGHT);
            
            //    UIKeyboardAnimationDurationUserInfoKey 获取键盘升起动画时间
            //[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
            //获取动画时间
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
            self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/14);
            [UIView commitAnimations];//开始执行动画
        }else if(login_passWordField.editing==YES){
            CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
            CGFloat  height = rect.size.height;
            NSLog(@"%f",height);
            
            //    UIKeyboardAnimationDurationUserInfoKey 获取键盘升起动画时间
            //[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
            //获取动画时间
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
            self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/14);
            [UIView commitAnimations];//开始执行动画
            
        }

    }
    

}
//重置、复原
- (void)KeyboardWillHide:(NSNotification *)sender{
    CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue];
    CGFloat  height = rect.size.height;
    NSLog(@"%f",height);
    [UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
    self.view.transform = CGAffineTransformIdentity;//重置状态
    [UIView commitAnimations];
    //    UITextField * textField = (id)[self.view viewWithTag:TextTag];
    //    textField.frame = CGRectMake(10, SCREEN_H - 45, SCREEN_W-20, 45);
    
}
上一篇下一篇

猜你喜欢

热点阅读