MBProgressHUD橫屏適配

在對程序橫屏適配的時候,發(fā)現(xiàn)MBProgressHUD會出現(xiàn)顯示方向異常
在豎屏狀態(tài)是正常顯示;
但在橫屏狀態(tài),方向還是豎屏狀態(tài),截圖如下:

豎屏狀態(tài).png
橫屏狀態(tài).png

在網(wǎng)上查找相關(guān)的資料,發(fā)現(xiàn)大部分都是提供在初始化的時候使用下列代碼

UIWindow *window = [[UIApplication sharedApplication] keyWindow];  
_hud = [[MBProgressHUD alloc] initWithWindow:window];  

但是在實(shí)際MBProgressHUD開源類中,無法找到相關(guān)的位置添加上述代碼。同時也沒有提供相關(guān)位置信息;基本為無用資料。

在閱讀MBProgressHUD.m 的過程中,發(fā)現(xiàn)這個函數(shù):

    - (void)setTransformForCurrentOrientation:(BOOL)animated {  
      // Stay in sync with the superview
      if (self.superview) {
        self.bounds = self.superview.bounds;
        [self setNeedsDisplay];
    }   
      UIInterfaceOrientation orientation = [UIApplication   sharedApplication].statusBarOrientation;
      NSLog(@"%ld", orientation);
    CGFloat radians = 0;
    if (UIInterfaceOrientationIsLandscape(orientation)) {
          if (orientation ==   UIInterfaceOrientationLandscapeLeft) { radians = -  (CGFloat)M_PI_2; }
        else { radians = (CGFloat)M_PI_2; }
        // Window coordinates differ!
        self.bounds = CGRectMake(0, 0,   self.bounds.size.height, self.bounds.size.width);
    } else {
        if (orientation ==   UIInterfaceOrientationPortraitUpsideDown) { radians =   (CGFloat)M_PI; } 
        else { radians = 0; }
      }
      rotationTransform =   CGAffineTransformMakeRotation(radians);
    
      if (animated) {
        [UIView beginAnimations:nil context:nil];
      }
      [self setTransform:rotationTransform];
      if (animated) {
        [UIView commitAnimations];
      }
     }

該方法主要內(nèi)容為:判斷當(dāng)前設(shè)備的方向,根據(jù)方向旋轉(zhuǎn)提示框。
通過斷點(diǎn)和控制臺輸出的方式,發(fā)現(xiàn)當(dāng)我的設(shè)備為橫屏狀態(tài),走進(jìn)了 if{}方法中,但是顯示效果卻是豎屏狀態(tài)。
郁悶的我猜想會不會已經(jīng)是正確的方向被擺歪了,于是我注釋了如下代碼塊

- (void)setTransformForCurrentOrientation:(BOOL)animated {  
// Stay in sync with the superview
  if (self.superview) {
    self.bounds = self.superview.bounds;
    [self setNeedsDisplay];
  }

  UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
  CGFloat radians = 0;
  /*if (UIInterfaceOrientationIsLandscape(orientation)) {
    if (orientation == UIInterfaceOrientationLandscapeLeft)   { radians = -(CGFloat)M_PI_2; }
    else { radians = (CGFloat)M_PI_2; }
    // Window coordinates differ!
    self.bounds = CGRectMake(0, 0,   self.bounds.size.height, self.bounds.size.width);
} else {
    if (orientation ==   UIInterfaceOrientationPortraitUpsideDown) { radians =   (CGFloat)M_PI; } 
    else { radians = 0; }
}*/
rotationTransform = CGAffineTransformMakeRotation(radians);
  if (animated) {
    [UIView beginAnimations:nil context:nil];
  }
  [self setTransform:rotationTransform];
  if (animated) {
    [UIView commitAnimations];
}
}

在實(shí)際測試中發(fā)現(xiàn),無論是橫屏狀態(tài)還是豎屏狀態(tài)以及在相互切換的過程中,均正常顯示。

猜想:在創(chuàng)建提示框的時候,實(shí)際上已經(jīng)根據(jù)當(dāng)前的屏幕方向創(chuàng)建正確的提示框,但是調(diào)用了上述方法(之前可能非正確方向,加入該方法),才會出現(xiàn)這樣的 BUG。

畢業(yè)沒多久,如有幫助,希望點(diǎn)個贊,加個關(guān)注。
如果有描述的不對的地方,歡迎批評指正,共同進(jìn)步。
除非注明,均為原創(chuàng),轉(zhuǎn)載請注明出處,謝謝

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,408評論 4 61
  • Swift版本點(diǎn)擊這里歡迎加入QQ群交流: 594119878最新更新日期:18-09-17 About A cu...
    ylgwhyh閱讀 26,155評論 7 249
  • 張清的日精進(jìn)第106天 體驗入 如果把日精進(jìn)看成是每天必須完成的工作遲早會感到厭煩,但如果把它當(dāng)做心聲的錄音機(jī),隨...
    kiyoi2017閱讀 176評論 0 1
  • 一直以來,我們的傳統(tǒng)文化過度看重文字的力量,對涂鴉這項技能往往都是視之不見,甚至對它懷有反感和質(zhì)疑的態(tài)度。 不難想...
    change_wjh閱讀 309評論 0 2
  • 1.什么是MongoDB MongoDB 是由C++語言編寫的,是一個基于分布式文件存儲的開源數(shù)據(jù)庫系統(tǒng)。在高負(fù)載...
    Py_Explorer閱讀 201評論 0 1

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