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开发中的小技巧,能大大方便我们的开发,持续更新。 1.UITableView的Group...
    无沣阅读 792评论 0 2
  • 工作了两年多,一直有个“坏习惯”,就是将工作中遇到的一些问题、技巧或心得记在印象笔记里面,按理来说,作为一个...
    F森阅读 2,034评论 3 26
  • 1、禁止手机睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa阅读 1,142评论 1 6
  • 淘宝一年一度的双十一狂欢节又要来到了,各大中小卖家都在忙着备战。大家都期待在双十一活动中能够有一个好的成绩...
    IMEdgar阅读 954评论 2 2
  • 这条路走了很久,很久, 久到漫野的荒芜变得繁华, 满天的璀璨变成金沙, 久到飞舞的时节, 青丝变成白发。 静静的等...
    落梅君阅读 183评论 3 1