[程序員日記]iOS 開發(fā)中遇到的問題(客戶端篇)

在項目開發(fā)中,自己遇到一些問題及解決方法,不斷更新中。

(1)UINavigationBarUITabBar上有一條橫線,是ShadowImage,默認是黑色的。在項目開發(fā)中,可以改變其圖片和顏色。在下圖個人熱點圖中可以看到導航欄下面的黑線。

[self.navigationController.navigationBar  setShadowImage:[UIImage imageWithColor:MyColor]];

(2) statusBar默認的高度是20.0f,在使用微信或者QQ通話,熱點等功能進入后臺時,statusBar的高度會變?yōu)?0.0f,下方的布局也會發(fā)生變化,在此要根據(jù)statusBar的變化調(diào)整布局,設置監(jiān)聽監(jiān)聽其變化。

打開熱點時`statusBar`高度變化

監(jiān)聽對象為UIApplicationDidChangeStatusBarFrameNotification

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(statusBarFrameWillChange:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];//添加監(jiān)聽

[[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationDidChangeStatusBarFrameNotification object:nil];//移除監(jiān)聽

當前statusBar的高度也是可以獲取的:

NSValue *rectValue = [notification.userInfo objectForKey:UIApplicationStatusBarFrameUserInfoKey];
CGRect statusRect = [rectValue CGRectValue];
CGRect statusFrame = [self.view convertRect:statusRect fromView:[[UIApplication sharedApplication]keyWindow]];
CGFloat statusHeight = statusFrame.size.height;

(3)在iPad開發(fā)時,使用iOS 9系統(tǒng)會出現(xiàn)tableviewCell的位置變化,在開發(fā)中默認與右側是15個像素,可是現(xiàn)在明顯大的多,這是因為在iOS 9后tableview的一個屬性發(fā)生了變化。

tableviewCell位置變化

需要調(diào)整

tableView.cellLayoutMarginsFollowReadableWidth = NO;

補充,因為方法是iOS9之后出現(xiàn)的,因此在調(diào)用時需要判斷系統(tǒng)是否大于9.0

if([UIDevice currentDevice].systemVersion.floatValue >= 9.0){
    tableView.cellLayoutMarginsFollowReadableWidth = NO;
}

(4)在做直播功能模塊,使用到 彈幕 的功能(彈幕使用第三方庫BarrageRenderer),彈幕為橫屏自動開啟,豎屏時關閉。在測試用發(fā)現(xiàn)彈幕有時開有時關,最終發(fā)現(xiàn)在屏幕橫放時無法顯示。原因是設置方法出現(xiàn)錯誤。

通過[UIDevice currentDevice].orientation 的狀態(tài)判斷橫豎屏會出現(xiàn)錯誤,因為其枚舉類型是UIDeviceOrientation,在查看其枚舉類型時會發(fā)現(xiàn)其除了Portrait LandscapeLeft LandscapeRight PortraitUpsideDown外還有FaceUp FaceDown兩個狀態(tài),忽略對其的設置會產(chǎn)生影響。

UIDeviceOrientation 枚舉

typedef NS_ENUM(NSInteger, UIDeviceOrientation) {
    UIDeviceOrientationUnknown,
    UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
    UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
    UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
    UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
    UIDeviceOrientationFaceUp,              // Device oriented flat, face up
    UIDeviceOrientationFaceDown             // Device oriented flat, face down
} __TVOS_PROHIBITED;

在對其使用時還可以將其轉(zhuǎn)換成UIInterfaceOrientation,因為后者只有常見的幾種類型。

UIDeviceOrientation orientation             = [UIDevice currentDevice].orientation;
UIInterfaceOrientation interfaceOrientation = (UIInterfaceOrientation)orientation;

UIInterfaceOrientation枚舉

typedef NS_ENUM(NSInteger, UIInterfaceOrientation) {
    UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} __TVOS_PROHIBITED;

另外在橫豎屏切換時我們會對屏幕的狀態(tài)做監(jiān)聽,通常監(jiān)聽的是UIDeviceOrientationDidChangeNotification,監(jiān)聽得到的結果是UIDeviceOrientation,也可以監(jiān)聽UIApplicationDidChangeStatusBarFrameNotification,得到UIInterfaceOrientation。當然UIApplicationDidChangeStatusBarFrameNotification也可以監(jiān)聽熱點等事件中出現(xiàn)的問題,如問題2.

(5)引入sdk報錯(非pod)

_res_9_ninit", referenced from: _setup_dns_server in QNResolver.o 
  1. 項目中需導入 libresolv.dylib或libresolv.9.dylib。(Build Phases --- Link Binary With Libraries);
  2. 或 (Build Settings --- Linking --- Other Linker Flags) 添加 -lresolv 選項

(6)在使用tableView時,使用footerView在最后一行默認不顯示最后一條橫線。簡單粗暴的方法,在cell中重寫layoutSubviews方法。

- (void)layoutSubviews {
    [super layoutSubviews];

    for (UIView *subview in self.contentView.superview.subviews) {
        if ([NSStringFromClass(subview.class) hasSuffix:@"SeparatorView"]) {
              subview.hidden = NO;
        }
    }
}
顯示tableView最后一行橫線

(7)項目中有視頻緩存,使用騰訊云下載完成下載功能,于是問題就來了,相信在做視頻緩存、下載功能的同行可能也會遇到這種問題。

視頻下載完成后,在 我的緩存中 查看,沒有問題,但當軟件更新或重新安裝,視頻列表還在,但視頻打不開了。

查看騰訊云視頻成功回調(diào),發(fā)現(xiàn)在返回的字典中,緩存文件的路徑是完整的。如圖。

查看文件路徑

查看文件路徑并重新安裝后會發(fā)現(xiàn)文件內(nèi)容沒變,但文件名變了。

重新安裝前
重新安裝后

這說明,app在重新安裝或升級后,會重新建一個文件,并將原來的文件全部copy過去,然后將原文件刪除。

知道原因之后就要解決了,當然解決的方法簡單粗暴,既然是文件名,那就進行拼接和裁剪了。在plist文件中存儲的是裁剪后的后半段路徑,而讀取文件路徑后再拼接上Caches地址。

獲取Caches目錄路徑的方法:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];

(8)iOS給好評時一般會直接打開app store應用詳情界面,其實也可以直接跳轉(zhuǎn)到評論頁面,更有利于引導用戶打分和評論。(將下面id改成自己的app id即可)

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=id1128294199&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"]];

直接跳轉(zhuǎn)到詳情

https://itunes.apple.com/cn/app/jia-zhang-mu-ke/id1128294199?mt=8

(9)鍵盤操作
測試發(fā)現(xiàn)一個問題,切換輸入法的時候鍵盤高度會上移,查找發(fā)現(xiàn),UIKeyboardWillShowNotification監(jiān)聽會調(diào)用多次,每次輸入法(如中英文)切換時會調(diào)用,從而調(diào)用相關修改布局的方法。

多次調(diào)用監(jiān)聽

初始時textField所在的view不顯示在視圖中,通過button的觸發(fā)才顯示,因此原監(jiān)聽后的調(diào)用方法:

  float time = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
  CGRect beginRect = [dic[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
  CGRect endRect = [dic[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  [UIView animateWithDuration:time animations:^{
      self.bottomView.hidden = NO;
      CGRect rect = self.bottomView.frame;
      rect.origin.y -= (beginRect.origin.y - endRect.origin.y+50);
      self.bottomView.frame = rect;
  }];

嘗試用值紀錄調(diào)用次數(shù),只記錄第一次調(diào)用發(fā)現(xiàn)也不可行,故將其view隱藏,通過按鈕的觸發(fā)顯示。

self.bottomView.hidden = NO;
rect.origin.y -= (beginRect.origin.y - endRect.origin.y);

[補充]
后來發(fā)現(xiàn)鍵盤高度會變化是由于IQKeybord惹的禍,將其禁用后使用原方法可以。

(10)UITextView回車發(fā)送
UITextField中有這樣一個代理函數(shù),可以實現(xiàn)鍵盤回車鍵發(fā)送的功能

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

但是UITextView中并沒有這個方法,所以需要判斷當前輸入的字符是否是回車,然后做出響應的處理。在這個函數(shù)的最后一個參數(shù)text代表你每次輸入的的那個字,所以:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:  (NSRange)range replacementText:(NSString *)text{
    if ([text isEqualToString:@"\n"]){
        return NO; 
    }

    return YES;
}

(11)TableView如何在初始化就選中某一行?
[self.tableView selectRowAtIndexPath:indexpath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; //選中第5行

(12)UISwitch顏色的改變
switchView.onTintColor = [UIColor colorWithRed:225/256.0 green:225/256.0 blue:225/256.0 alpha:1];
switchView.thumbTintColor = DEF_LightBlueColor;

更多問題后續(xù)補充,歡迎探討指正

文章優(yōu)先發(fā)表于:http://keyliu.com
轉(zhuǎn)載請注明出處。

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容

  • 概述在iOS開發(fā)中UITableView可以說是使用最廣泛的控件,我們平時使用的軟件中到處都可以看到它的影子,類似...
    liudhkk閱讀 9,091評論 3 38
  • 作業(yè)一 作業(yè)二
  • 最近陸續(xù)看了「次第花開」,「透過佛法看世界」以及「正念的奇跡」,獲益匪淺,被佛法的廣闊無邊和博大精深深深吸引。我做...
    寂顏慕菲閱讀 790評論 0 1
  • 時間,太多說不清與道不明。 曾經(jīng)扒著不放的等回頭,如今面對回頭,心情或許是那時候換位的一種體驗,只是在換位中不公平...
    半顆西柚閱讀 333評論 0 0