1.屏幕從橫屏轉(zhuǎn)到豎屏?xí)r板壮,UICollectionViewFlowLayout產(chǎn)生警告
背景:UICollectionView的每個(gè)item的大小為collection view自身的bounds的大小擂仍。
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize size = self.collectionView.frame.size;
return size;
}
控制臺(tái)中出現(xiàn)的警告信息:
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.
此警告信息是無(wú)害的,并不會(huì)影響應(yīng)用的正常運(yùn)行。但是辜妓,這樣的警告信息存在勺疼,對(duì)于強(qiáng)迫癥的人來(lái)說(shuō)是不可接受的。
解決辦法:override View Controller下面的方法
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator {
[super viewWillTransitionToSize: size withTransitionCoordinator: coordinator];
//調(diào)用這一行代碼可以去除警告
[self.collectionView.collectionViewLayout invalidateLayout];
}
2.UICollectionViewCell中的動(dòng)畫(huà)页慷,在點(diǎn)擊Cell進(jìn)行頁(yè)面跳轉(zhuǎn)后憔足,動(dòng)畫(huà)消失
原因:在頁(yè)面跳轉(zhuǎn)時(shí)胁附,添加在CALayer對(duì)象上的動(dòng)畫(huà)會(huì)被移除。UIKit內(nèi)部是這么實(shí)現(xiàn)的滓彰。
解決辦法:使用Core Animation來(lái)做動(dòng)畫(huà)症杏,并且關(guān)鍵的一點(diǎn)是瓷式,要將removedOnCompletion屬性的值設(shè)為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功能時(shí)出錯(cuò),無(wú)法抓取視圖層級(jí)
目前碰到的情況是司抱,在控制臺(tái)中會(huì)打印出如下信息:Assertion failure in -[UITextView _firstBaselineOffsetFromTop], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.60.7/UITextView.m:1683
解決辦法:為UITextView添加一個(gè)分類,重寫(xiě)如下兩個(gè)方法:
@implementation UITextView (VisualDebugger)
#if DEBUG
- (void)_firstBaselineOffsetFromTop {
}
- (void)_baselineOffsetFromBottom {
}
#endif
@end
注意片择,上述代碼應(yīng)該只在DEBUG版本中才生效却盘,以免對(duì)正式版本造成影響。
4. 從屏幕左邊緣右滑返回時(shí)邦蜜,獲取interactivePopGestureRecognizer的progress
當(dāng)一個(gè)視圖控制器被push到navigation controller以后依鸥,可以通過(guò)在屏幕左邊緣右滑的手勢(shì),將視圖控制器pop悼沈。在這個(gè)過(guò)程中贱迟,如果想獲取progress,可以使用如下代碼絮供。
//在viewDidLoad里添加下面一行代碼
[self.navigationController.interactivePopGestureRecognizer addTarget: self action: @selector(handlePopGesture)];
- (void)handlePopGesture {
NSLog(@"progress is %f", self.transitionCoordinator.percentComplete);
}