iOS开发iOS tipsiOS 深度好文

iOS开发笔记-43:关于手机视图的放大模式还是标准模式的判断

2017-06-08  本文已影响1398人  原味蛋炒饭

因为手机视图的放大和标准模式,屏幕宽度不同引起collectionView cell之间有间距。这个问题引起collectionView适配问题的解决方法贴在最后。

首先是判断不同的模式:

CGRect bounds = [[UIScreen mainScreen] bounds];
NSString *screenMode = [[UIScreen mainScreen].coordinateSpace description];
CGFloat scale = [[UIScreen mainScreen] scale];
CGFloat nativeScale = [[UIScreen mainScreen] nativeScale];

NSLog(@"\n bounds: %@\n screen mode: %@\n scale: %f\n native scale: %f", NSStringFromCGRect(bounds), screenMode, scale, nativeScale);
iPhone6标准模式下输出:

bounds: { {0, 0}, {375, 667} }
screen mode: <UIScreen: 0x155603cd0; bounds = { {0, 0}, {375, 667} }; mode = <UIScreenMode: 0x170021f40; size = 750.000000 x 1334.000000>>
scale: 2.000000
native scale: 2.000000
iPhone6放大模式下输出:

bounds: { {0, 0}, {320, 568} }
screen mode: <UIScreen: 0x12ee03d40; bounds = { {0, 0}, {320, 568} }; mode = <UIScreenMode: 0x1700256e0; size = 640.000000 x 1136.000000>>
scale: 2.000000
native scale: 2.343750
iPhone6+标准模式下输出:

bounds: { {0, 0}, {414, 736} }
screen mode: <UIScreen: 0x13ee01840; bounds = { {0, 0}, {414, 736} }; mode = <UIScreenMode: 0x17002b4e0; size = 1242.000000 x 2208.000000>>
scale: 3.000000
native scale: 2.608696
iPhone6+放大模式下输出:

bounds: { {0, 0}, {375, 667} }
screen mode: <UIScreen: 0x13f6021f0; bounds = { {0, 0}, {375, 667} }; mode = <UIScreenMode: 0x170028c20; size = 1125.000000 x 2001.000000>>
scale: 3.000000
native scale: 2.880000
iPhone7标准模式下输出:

bounds: {{0, 0}, {375, 667}}
 screen mode: <UIScreen: 0x101700ef0; bounds = {{0, 0}, {375, 667}}; mode = <UIScreenMode: 0x170233120; size = 750.000000 x 1334.000000>>
 scale: 2.000000
 native scale: 2.000000
iPhone7放大模式下输出:

 bounds: {{0, 0}, {320, 568}}
 screen mode: <UIScreen: 0x101906b50; bounds = {{0, 0}, {320, 568}}; mode = <UIScreenMode: 0x17023e920; size = 640.000000 x 1136.000000>>
 scale: 2.000000
 native scale: 2.343750

collectionView适配的解决方案:

首先这是网上,利用控件本身的方法来调节
应该是调节UICollectionViewFlowLayout的minimumInteritemSpacing属性,
这个是调节同一行的cell之间的距离的。

使用-(CGFloat )collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section;
或直接设置layout.minimumInteritemSpacing = 0;
但还要配合cell的大小、section左右缩进,因为collectionView默认section左右缩进左右是0,然后cell的宽度小的话,因为要保持cell宽度不变,这样cell之间还是有间隔。
用故事板拖一个collectionView,调一调很直观

我在上面方法无效的情况下,想了一个笨办法:
就是比如屏幕宽度为320,那么cell宽度为106,106,108

CGFloat ScreenWidth = 屏幕宽度;
CGFloat width = 0;
            
 if ((indexPath.row % 3) == 0) {
      width = (ScreenWidth - (int)(ScreenWidth/3.0) - (int)(ScreenWidth/3.0));
                
}else {
      width = (int)(ScreenWidth/3.0);
}
CGFloat height = (int)(ScreenWidth/3.0);
CGSize cellSize = CGSizeMake(width, height);
上一篇下一篇

猜你喜欢

热点阅读