iOS開(kāi)發(fā)小問(wèn)題總結(jié)

在開(kāi)發(fā)過(guò)程中,我們避免不了一些小問(wèn)題的出現(xiàn),現(xiàn)在為了方便之后的查找,將這些小問(wèn)題進(jìn)行持續(xù)總結(jié)更新。那我們開(kāi)始總結(jié)吧。

  • 修改導(dǎo)航欄的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;
  • 調(diào)用系統(tǒng)電話方式(推薦)
    UIWebView *callWebView = [[UIWebView alloc] init];
    NSURL *telURL = [NSURL URLWithString:@"tel:10086"];
    [callWebView loadRequest:[NSURLRequest requestWithURL:telURL]];
    [self.view addSubview:callWebView];
  • 鍵盤(pán)彈起收回監(jiān)聽(tīng)方法
 //監(jiān)聽(tīng)鍵盤(pán)
    //官方定義好的UIKeyboardWillShowNotification 通知名稱
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
//兩個(gè)方法(當(dāng)鍵盤(pán)彈起、當(dāng)鍵盤(pán)收回)
-(void)KeyboardWillShow:(NSNotification *)sender;
- (void)KeyboardWillHide:(NSNotification *)sender;
  • 調(diào)用攝像頭或相冊(cè)
// 打開(kāi)相機(jī)
- (void)openCamera {
    // UIImagePickerControllerCameraDeviceRear 后置攝像頭
    // UIImagePickerControllerCameraDeviceFront 前置攝像頭
    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    if (!isCamera) {
        NSLog(@"沒(méi)有攝像頭");
        return ;
    }
     
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.delegate = self;
    // 編輯模式
    imagePicker.allowsEditing = YES;
     
    [self  presentViewController:imagePicker animated:YES completion:^{
    }];
 
     
}
 
 
// 打開(kāi)相冊(cè)
- (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];
     
}
 
 
 
// 取消相冊(cè)
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:NULL];
 
}
  • 禁止項(xiàng)目橫屏
將general里面的Device Orientation里的后三個(gè)選項(xiàng)去掉。
  • 調(diào)整彈出鍵盤(pán)時(shí)的界面上移高度。(保證界面效果基本一致,判斷屏幕大小來(lái)區(qū)分設(shè)備)

調(diào)整代碼如下:
    //監(jiān)聽(tīng)鍵盤(pán)
    //官方定義好的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 獲取鍵盤(pán)升起動(dòng)畫(huà)時(shí)間
            //[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
            //獲取動(dòng)畫(huà)時(shí)間
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
            self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/6);
            [UIView commitAnimations];//開(kāi)始執(zhí)行動(dòng)畫(huà)
        }else if(login_passWordField.editing==YES){
            CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
            CGFloat  height = rect.size.height;
            NSLog(@"%f",height);
            
            //    UIKeyboardAnimationDurationUserInfoKey 獲取鍵盤(pán)升起動(dòng)畫(huà)時(shí)間
            //[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
            //獲取動(dòng)畫(huà)時(shí)間
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
            self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/6);
            [UIView commitAnimations];//開(kāi)始執(zhí)行動(dòng)畫(huà)
            
        }

    }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 獲取鍵盤(pán)升起動(dòng)畫(huà)時(shí)間
            //[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
            //獲取動(dòng)畫(huà)時(shí)間
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
            self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/14);
            [UIView commitAnimations];//開(kāi)始執(zhí)行動(dòng)畫(huà)
        }else if(login_passWordField.editing==YES){
            CGRect rect = [[sender.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
            CGFloat  height = rect.size.height;
            NSLog(@"%f",height);
            
            //    UIKeyboardAnimationDurationUserInfoKey 獲取鍵盤(pán)升起動(dòng)畫(huà)時(shí)間
            //[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]]
            //獲取動(dòng)畫(huà)時(shí)間
            [UIView beginAnimations:nil context:nil];
            [UIView setAnimationDuration:[[sender.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]];
            self.view.transform = CGAffineTransformMakeTranslation(0, -HEIGHT/14);
            [UIView commitAnimations];//開(kāi)始執(zhí)行動(dòng)畫(huà)
            
        }

    }
    

}
//重置、復(fù)原
- (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;//重置狀態(tài)
    [UIView commitAnimations];
    //    UITextField * textField = (id)[self.view viewWithTag:TextTag];
    //    textField.frame = CGRectMake(10, SCREEN_H - 45, SCREEN_W-20, 45);
    
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 原文 在這里總結(jié)一些iOS開(kāi)發(fā)中的小技巧,能大大方便我們的開(kāi)發(fā),持續(xù)更新。 1.UITableView的Group...
    無(wú)灃閱讀 853評(píng)論 0 2
  • 工作了兩年多,一直有個(gè)“壞習(xí)慣”,就是將工作中遇到的一些問(wèn)題、技巧或心得記在印象筆記里面,按理來(lái)說(shuō),作為一個(gè)...
    F森閱讀 2,159評(píng)論 3 26
  • 1、禁止手機(jī)睡眠[UIApplication sharedApplication].idleTimerDisabl...
    DingGa閱讀 1,206評(píng)論 1 6
  • 淘寶一年一度的雙十一狂歡節(jié)又要來(lái)到了,各大中小賣家都在忙著備戰(zhàn)。大家都期待在雙十一活動(dòng)中能夠有一個(gè)好的成績(jī)...
    IMEdgar閱讀 1,115評(píng)論 2 2
  • 這條路走了很久,很久, 久到漫野的荒蕪變得繁華, 滿天的璀璨變成金沙, 久到飛舞的時(shí)節(jié), 青絲變成白發(fā)。 靜靜的等...
    落梅君閱讀 196評(píng)論 3 1

友情鏈接更多精彩內(nèi)容