项目中遇到的问题及解决办法总结

1.屏幕从横屏转到竖屏时,UICollectionViewFlowLayout产生警告

背景:UICollectionView的每个item的大小为collection view自身的bounds的大小。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = self.collectionView.frame.size;
    return size;
}
控制台中出现的警告信息:

2018-01-16 16:35:56.126218+0800 TestTabbar[9496:378585] The behavior of the UICollectionViewFlowLayout is not defined because:
2018-01-16 16:35:56.126359+0800 TestTabbar[9496:378585] the item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values.
2018-01-16 16:35:56.126850+0800 TestTabbar[9496:378585] The relevant UICollectionViewFlowLayout instance is <UICollectionViewFlowLayout: 0x7f9310502d70>, and it is attached to <UICollectionView: 0x7f931480de00; frame = (0 0; 414 736); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x60400044fa80>; animations = { position=<CABasicAnimation: 0x614000239940>; bounds.origin=<CABasicAnimation: 0x614000239a00>; bounds.size=<CABasicAnimation: 0x614000239a20>; }; layer = <CALayer: 0x608000030b60>; contentOffset: {0, 0}; contentSize: {736, 4140}; adjustedContentInset: {0, 0, 0, 0}> collection view layout: <UICollectionViewFlowLayout: 0x7f9310502d70>.
2018-01-16 16:35:56.126957+0800 TestTabbar[9496:378585] Make a symbolic breakpoint at UICollectionViewFlowLayoutBreakForInvalidSizes to catch this in the debugger.

此警告信息是无害的,并不会影响应用的正常运行。但是,这样的警告信息存在,对于强迫症的人来说是不可接受的。

解决办法:override View Controller下面的方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
    [super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
    //调用这一行代码可以去除警告
    [self.collectionView.collectionViewLayout invalidateLayout];
}

2.UICollectionViewCell中的动画,在点击Cell进行页面跳转后,动画消失

原因:在页面跳转时,添加在CALayer对象上的动画会被移除。UIKit内部是这么实现的。
解决办法:使用Core Animation来做动画,并且关键的一点是,要将removedOnCompletion属性的值设为NO。
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"position"];
    animation.fromValue = [NSValue valueWithCGPoint: testView.layer.position];
    animation.toValue = [NSValue valueWithCGPoint: CGPointMake(testView.layer.position.x, testView.layer.position.y + 100)];
    animation.duration = 2;
    animation.repeatCount = INT_MAX;
    animation.autoreverses = YES;
    animation.removedOnCompletion = NO;
    [testView.layer addAnimation: animation forKey: nil];

3. 使用Xcode的View Debugging功能时出错,无法抓取视图层级

目前碰到的情况是,在控制台中会打印出如下信息:Assertion failure in -[UITextView _firstBaselineOffsetFromTop], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UITextView.m:1683

解决办法:为UITextView添加一个分类,重写如下两个方法:
@implementation UITextView (VisualDebugger)

#if DEBUG
- (void)_firstBaselineOffsetFromTop {
}

- (void)_baselineOffsetFromBottom {
}
#endif

@end

注意,上述代码应该只在DEBUG版本中才生效,以免对正式版本造成影响。

4. 从屏幕左边缘右滑返回时,获取interactivePopGestureRecognizer的progress

当一个视图控制器被push到navigation controller以后,可以通过在屏幕左边缘右滑的手势,将视图控制器pop。在这个过程中,如果想获取progress,可以使用如下代码。

//在viewDidLoad里添加下面一行代码
[self.navigationController.interactivePopGestureRecognizer addTarget: self action: @selector(handlePopGesture)];

- (void)handlePopGesture {
    NSLog(@"progress is %f", self.transitionCoordinator.percentComplete);
}