在對程序橫屏適配的時候割坠,發(fā)現(xiàn)MBProgressHUD會出現(xiàn)顯示方向異常
在豎屏狀態(tài)是正常顯示;
但在橫屏狀態(tài)妒牙,方向還是豎屏狀態(tài)彼哼,截圖如下:
豎屏狀態(tài).png
橫屏狀態(tài).png
在網上查找相關的資料,發(fā)現(xiàn)大部分都是提供在初始化的時候使用下列代碼
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
_hud = [[MBProgressHUD alloc] initWithWindow:window];
但是在實際MBProgressHUD開源類中湘今,無法找到相關的位置添加上述代碼敢朱。同時也沒有提供相關位置信息;基本為無用資料摩瞎。
在閱讀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];
}
}
該方法主要內容為:判斷當前設備的方向,根據方向旋轉提示框愉豺。
通過斷點和控制臺輸出的方式篓吁,發(fā)現(xiàn)當我的設備為橫屏狀態(tài),走進了 if{}方法中蚪拦,但是顯示效果卻是豎屏狀態(tài)杖剪。
郁悶的我猜想會不會已經是正確的方向被擺歪了,于是我注釋了如下代碼塊
- (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];
}
}
在實際測試中發(fā)現(xiàn)驰贷,無論是橫屏狀態(tài)還是豎屏狀態(tài)以及在相互切換的過程中盛嘿,均正常顯示。
猜想:在創(chuàng)建提示框的時候括袒,實際上已經根據當前的屏幕方向創(chuàng)建正確的提示框次兆,但是調用了上述方法(之前可能非正確方向,加入該方法)锹锰,才會出現(xiàn)這樣的 BUG芥炭。
畢業(yè)沒多久,如有幫助恃慧,希望點個贊园蝠,加個關注。
如果有描述的不對的地方痢士,歡迎批評指正彪薛,共同進步。
除非注明怠蹂,均為原創(chuàng)善延,轉載請注明出處,謝謝