iOS开发小问题总结

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

  • 修改导航栏的title颜色
在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);
    
}

你可能感兴趣的:(iOS开发小问题总结)