在項目開發中,自己遇到一些問題及解決方法,不斷更新中。
(1)UINavigationBar
和UITabBar
上有一條橫線,是ShadowImage
,默認是黑色的。在項目開發中,可以改變其圖片和顏色。在下圖個人熱點圖中可以看到導航欄下面的黑線。
[self.navigationController.navigationBar setShadowImage:[UIImage imageWithColor:MyColor]];
(2) statusBar
默認的高度是20.0f,在使用微信或者QQ通話,熱點等功能進入后臺時,statusBar
的高度會變為40.0f,下方的布局也會發生變化,在此要根據statusBar
的變化調整布局,設置監聽監聽其變化。
監聽對象為UIApplicationDidChangeStatusBarFrameNotification
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(statusBarFrameWillChange:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];//添加監聽
[[NSNotificationCenter defaultCenter]removeObserver:self name:UIApplicationDidChangeStatusBarFrameNotification object:nil];//移除監聽
當前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開發時,使用iOS 9系統會出現tableviewCell
的位置變化,在開發中默認與右側是15個像素,可是現在明顯大的多,這是因為在iOS 9后tableview
的一個屬性發生了變化。
需要調整
tableView.cellLayoutMarginsFollowReadableWidth = NO;
補充,因為方法是iOS9之后出現的,因此在調用時需要判斷系統是否大于9.0
if([UIDevice currentDevice].systemVersion.floatValue >= 9.0){
tableView.cellLayoutMarginsFollowReadableWidth = NO;
}
(4)在做直播功能模塊,使用到 彈幕 的功能(彈幕使用第三方庫BarrageRenderer
),彈幕為橫屏自動開啟,豎屏時關閉。在測試用發現彈幕有時開有時關,最終發現在屏幕橫放時無法顯示。原因是設置方法出現錯誤。
通過[UIDevice currentDevice].orientation
的狀態判斷橫豎屏會出現錯誤,因為其枚舉類型是UIDeviceOrientation
,在查看其枚舉類型時會發現其除了Portrait
LandscapeLeft
LandscapeRight
PortraitUpsideDown
外還有FaceUp
FaceDown
兩個狀態,忽略對其的設置會產生影響。
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;
在對其使用時還可以將其轉換成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;
另外在橫豎屏切換時我們會對屏幕的狀態做監聽,通常監聽的是UIDeviceOrientationDidChangeNotification
,監聽得到的結果是UIDeviceOrientation
,也可以監聽UIApplicationDidChangeStatusBarFrameNotification
,得到UIInterfaceOrientation
。當然UIApplicationDidChangeStatusBarFrameNotification
也可以監聽熱點等事件中出現的問題,如問題2.
(5)引入sdk報錯(非pod)
_res_9_ninit", referenced from: _setup_dns_server in QNResolver.o
- 項目中需導入 libresolv.dylib或libresolv.9.dylib。(Build Phases --- Link Binary With Libraries);
- 或 (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;
}
}
}
(7)項目中有視頻緩存,使用騰訊云下載完成下載功能,于是問題就來了,相信在做視頻緩存、下載功能的同行可能也會遇到這種問題。
視頻下載完成后,在 我的緩存中 查看,沒有問題,但當軟件更新或重新安裝,視頻列表還在,但視頻打不開了。
查看騰訊云視頻成功回調,發現在返回的字典中,緩存文件的路徑是完整的。如圖。
查看文件路徑并重新安裝后會發現文件內容沒變,但文件名變了。
這說明,app在重新安裝或升級后,會重新建一個文件,并將原來的文件全部copy過去,然后將原文件刪除。
知道原因之后就要解決了,當然解決的方法簡單粗暴,既然是文件名,那就進行拼接和裁剪了。在plist
文件中存儲的是裁剪后的后半段路徑,而讀取文件路徑后再拼接上Caches
地址。
獲取Caches
目錄路徑的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachesDir = [paths objectAtIndex:0];
(8)iOS給好評時一般會直接打開app store應用詳情界面,其實也可以直接跳轉到評論頁面,更有利于引導用戶打分和評論。(將下面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"]];
直接跳轉到詳情
https://itunes.apple.com/cn/app/jia-zhang-mu-ke/id1128294199?mt=8
(9)鍵盤操作
測試發現一個問題,切換輸入法的時候鍵盤高度會上移,查找發現,UIKeyboardWillShowNotification
監聽會調用多次,每次輸入法(如中英文)切換時會調用,從而調用相關修改布局的方法。
初始時textField
所在的view
不顯示在視圖中,通過button
的觸發才顯示,因此原監聽后的調用方法:
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;
}];
嘗試用值紀錄調用次數,只記錄第一次調用發現也不可行,故將其view隱藏,通過按鈕的觸發顯示。
self.bottomView.hidden = NO;
rect.origin.y -= (beginRect.origin.y - endRect.origin.y);
[補充]
后來發現鍵盤高度會變化是由于IQKeybord惹的禍,將其禁用后使用原方法可以。
(10)UITextView
回車發送
在UITextField
中有這樣一個代理函數,可以實現鍵盤回車鍵發送的功能
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
但是UITextView
中并沒有這個方法,所以需要判斷當前輸入的字符是否是回車,然后做出響應的處理。在這個函數的最后一個參數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;
更多問題后續補充,歡迎探討指正
文章優先發表于:http://keyliu.com
轉載請注明出處。