说在前面
最近针对公司项目进行了iPad的适配,发现了很多有关屏幕旋转的适配,发现了一些有趣的问题.
UIcollectionView在屏幕旋转的过程中,没有进行自动适配,也就是旋转的过程中,collectionView的UIcollectionViewDelegateFlowLayout并没有重新出发调用.
解决方案:页面添加屏幕旋转通知,判断有效旋转后,刷新页面,调用invalidateLayout.
代码附上:
extension UIcollectionView {
// 刷新数据
func safeReloadData() {
CATransaction.begin()
CATransaction.setDisableActions(true)
self.reloadData()
CATransaction.commit()
}
// 重新布局
func safeLayoutInvalidateLayout() {
CATransaction.begin()
CATransaction.setDisableActions(true)
self.collectionViewLayout.invalidateLayout()
CATransaction.commit()
}
}
下面是基于BaseView写的,其中判断,项目需求,只有iPad支持倒屏状态.
子类中,需要屏幕旋转的,设置isOpenDeviceOrientationObserver为YES就行,子类实现ktx_deviceOrientationDidChange方法即可
代码中,判断的是有效屏幕的旋转,也就是横屏和竖屏的旋转,对平放等不做考虑,如果你的项目需要,则另行修改.
#import
NS_ASSUME_NONNULL_BEGIN
@interface TTBaseView : UIView
/// 开启屏幕旋转通知
@property (nonatomic, assign) BOOL isOpenDeviceOrientationObserver;
// 屏幕旋转
- (void)ktx_deviceOrientationDidChange;
@end
NS_ASSUME_NONNULL_END
#import "TTBaseView.h"
#import "UIDevice+TTDevice.h"
@interface TTBaseView()
// 是不是水平
@property (nonatomic, assign) BOOL reocrdIsLaunch;
@end
@implementation TTBaseView
@synthesize isOpenDeviceOrientationObserver = _isOpenDeviceOrientationObserver;
- (instancetype)init {
if (self = [super init]) {
UIDevice *device = [UIDevice currentDevice] ;
self.reocrdIsLaunch = (device.orientation == UIDeviceOrientationLandscapeLeft) || (device.orientation == UIDeviceOrientationLandscapeRight);
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
UIDevice *device = [UIDevice currentDevice] ;
self.reocrdIsLaunch = (device.orientation == UIDeviceOrientationLandscapeLeft) || (device.orientation == UIDeviceOrientationLandscapeRight);
}
- (void)dealloc {
[self setIsOpenDeviceOrientationObserver:NO];
}
- (void)setIsOpenDeviceOrientationObserver:(BOOL)isOpenDeviceOrientationObserver {
BOOL oldValue = self.isOpenDeviceOrientationObserver;
if (oldValue == isOpenDeviceOrientationObserver) {
return;
}
_isOpenDeviceOrientationObserver = isOpenDeviceOrientationObserver;
if (_isOpenDeviceOrientationObserver) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
} else {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
}
- (void)deviceOrientationDidChange {
//1.获取 当前设备 实例
UIDevice *device = [UIDevice currentDevice] ;
BOOL is_need_update= NO;
switch (device.orientation) {
case UIDeviceOrientationPortraitUpsideDown:
if (![UIDevice isPad]) {
break;
}
case UIDeviceOrientationPortrait:
if (self.reocrdIsLaunch) {
is_need_update = YES;
self.reocrdIsLaunch = NO;
}
break;
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
if (!self.reocrdIsLaunch) {
is_need_update = YES;
self.reocrdIsLaunch = YES;
}
break;
default:
// NSLog(@"无法辨识");
break;
}
if (is_need_update) {
[self ktx_deviceOrientationDidChange];
}
}
- (void)ktx_deviceOrientationDidChange {
}
这个问题其实比较常见,这个也是屏幕旋转的的问题.
涉及到屏幕旋转,有关UICollectionView的itemsize的计算,获取到的数值会不准确,会受到屏幕旋转的延迟影响,也就是说,当页面已经转过来后,itemsize的获取还是原屏幕状态的值.
有关UICollectionView的itemsize的计算, 不要取collectionView.width or self.view,
获取当前屏幕的宽度或者高度,:[[UIApplication sharedApplication].delegate window].width
ps:直接写出来.width or .height 自己写个view的分类
UITableView是属于列表类型,所以,默认的宽度是根据屏幕进行自动适配的,高度不会,所以针对cell的子元素的换行的高度还需要特殊处理.
上面提到的屏幕旋转的问题,基本上也同样适用于UITableView.