【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;

参考:博客四博客五

猜你喜欢

转载自www.cnblogs.com/weicyNo-1/p/10103467.html