1. 程式人生 > >【iOS】開發中遇到的小知識點

【iOS】開發中遇到的小知識點

1.純程式碼寫collectionViewCell

如上所述,近期我一直使用純程式碼寫工程,在建立collectionViewCell時遇到了一個小問題。

純程式碼在tableViewCell中我們使用下面的方法來新增子檢視。

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle 
= UITableViewCellSelectionStyleNone; [self createSubViews]; } return self; }

可是在collectionViewCell中沒有這個方法。我試了init、initWithCoder方法都不能新增子檢視。後來才發現它走的是frame。應該呼叫下面的方法。

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self createSubViews];
    }
    
return self; }

SDCycleScrollView中也是這樣建立的。詳見SelectVideoImageCollectionViewCell中。

個人推測因為collectionViewCell有具體的大小,會呼叫這個方法。

 

2.禁止某個單獨頁面側滑返回。

在iOS中有側滑返回的功能,但是在某些介面中我們不需要這個功能。那麼如何單獨禁止呢?

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled 
= NO; } } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; if([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) { self.navigationController.interactivePopGestureRecognizer.enabled = YES; }; }

參考:部落格一部落格二部落格三

這個方法就可以實現這個功能了,但是如果你使用了一下自定義的導航欄可能會不管用。就像我使用了RTRootNavigationController這個第三方導航欄管理。使用了上面的方法不能實現這個功能。後來看原始碼發現,作者已經考慮了這個問題直接使用原始碼中的屬性方法就可以了。

self.rt_disableInteractivePop = true;
self.rt_navigationController.rt_disableInteractivePop = true;

參考:部落格四部落格五