iOS小問題(一)

前言

這個系列就是隨手記錄一下平時捧到的一些問題和一些嘗試。
tips:如果有錯誤,或者有更好的詳細解答,請隨時聯系我進行修改。

解決的問題

1.statusbar顏色問題

info.plist文件中,“View controller-based status bar appearance”項設為YES,則View controller對status bar的設置優先級高于application的設置。為NO則以application的設置為準,view controller的prefersStatusBarHidden方法無效,是根本不會被調用的。

意思就是View controller-based status bar appearance為YES:

[[UIApplication sharedApplication]*setStatusBarStyle*:UIStatusBarStyleLightContent];

無效,只能使用UIViewController的方法

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

設為no則反過來。

另外,當UIViewController自定義navigationController,并且navigationController同樣繼承該方法,UIViewController該方法無效。在navigationController中定義下面方法,則uiviewcontroller有效

- (UIViewController *)childViewControllerForStatusBarStyle {
    return [self.childViewControllers lastObject];
}

2.webview NSURLErrorServerCertificateUntrusted 問題

webview加載某些頁面,證書不可信,報1202error code,可用如下方法:

#pragma mark UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
{
    NSString *url = request.URL.absoluteString;
    if (url.length == 0 || ![url hasPrefix:@"http"])
        return NO;
    _request = request;
    NSLog(@"paypal web url : %@",url);
    return YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error{
    if ( error.code == NSURLErrorServerCertificateUntrusted ){
        _urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];
        [_urlConnection start];
    }
}

#pragma mark - NURLConnection delegate

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge previousFailureCount] == 0)
    {
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
    } else
    {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [_urlConnection cancel];
    _urlConnection = nil;
    [_webView loadRequest:_request];
}

// We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

3.ui小細節

  • 1.對于uiviewcontroller,ui初始化位置,viewdidload好過init,因為會有self.view的大小問題。
  • 2.autoresizingMask適合給cell使用,或者是類似初始化讀self.view之類的寬度讀出來是默認320的地方。
  • 3.parentview有autoresizingMask變化,但子view不會跟著變化。所以很多系統ui包含子view的,需要注意一下(比如uiwebview)。
  • 4.在iOS 7中,蘋果引入了一個新的屬性,叫做[UIViewController setEdgesForExtendedLayout:],它的默認值為UIRectEdgeAll。當你的容器是navigation controller時,默認的布局將從navigation bar的頂部開始。這就是為什么所有的UI元素都往上漂移了44pt。
    修復這個問題的快速方法就是在方法- (void)viewDidLoad中添加如下一行代碼:
    self.edgesForExtendedLayout = UIRectEdgeNone;
  • 5.UILabel的sizeToFitautoresizingMask = UIViewAutoresizingFlexibleWidth可能存在沖突。

4.iOS10的無限layoutsubviews問題

iOS10下,在cell中使用layoutsubviews,中如果2次設置cell的frame,并且2次frame有不同,就會無限調用layoutsubviews,從而卡死。
其他iOS系統沒這問題,目測是iOS10測試版本的bug。

5.autoresizingMask與layoutsubviews生效時機問題

autoresizingMask先生效,然后生效layoutsubviews。

6.VIEW DEBUG時候,碰到的_UIView系列都是系統的UI

比如tableview出現的_UITableViewSeparatorView

7.UILabel sizeToFit會根據當前的view width進行fit,所以第一次很短的text會導致uilabel的view width變小,這個時候如果有一個長text,要重新設置view width再sizeToFit

11.8更新

8.系統默認uitableview如果不去掉分割線,separateView會有0.5dp(1px)的高度。所以cell內的contentview的高度會差cell的高度0.5dp。

未解決的問題

未解決的問題,如果有人知曉答案的話,麻煩評論指點一下。
1.key不存在在strings文件中,NSLocalizedStringFromTable未找到key的時候,返回的key會從小寫變成大寫
key不存在在strings文件中,用NSLocalizedStringFromTable去尋找某個key。
strings文件中所有key為小寫,但在某些分支上,返回值會變成key的大寫。但在其他分支上不會。

參考鏈接:

本文csdn地址
1.[iOS]關于狀態欄(UIStatusBar)的若干問題
2.stackoverflow

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

推薦閱讀更多精彩內容