iOS 11 下 UICollectionView 出現(xiàn)滾動條被 HeaderView 遮擋的問題

iOS 11 下 UICollectionView 出現(xiàn)滾動條被 HeaderView 遮擋的問題

在使用了- collectionView: viewForSupplementaryElementOfKind: atIndexPath:的 UICollectionView 頁面中,滑動頁面的時候滾動條會被 HeaderView 遮擋。導(dǎo)致滾動條看起來是斷斷續(xù)續(xù)的。

問題頁面如下圖所示(查看滾動條):

以上問題具體是否與使用了 - collectionView: viewForSupplementaryElementOfKind: atIndexPath: 有關(guān)目前還不確定,待驗證。

這個問題在之前的 iOS 10 上是沒有的,iOS 11 新出之后才出現(xiàn)。經(jīng)過在 stackoverflow 上查找之后找到解決辦法。https://stackoverflow.com/questions/46694144/scrollbar-incorrectly-appears-underneath-uicollectionview-section-header

stackoverflow 中提供的是 swift 中的解決辦法,我自己則使用的是 Objective-C。

提示:解決這個問題只是更改了繼承自 UICollectionReusableView 的自定義 HeaderView 類文件,所以這里只貼該自定義 HeaderView 的代碼。

先看看在修復(fù)問題之前的 CustomHeaderView 類文件代碼

// CustomHeaderView.h

#import <UIKit/UIKit.h>

extern NSString *const CustomHeaderViewReuseIdentifier;

@interface CustomHeaderView : UICollectionReusableView

@property (nonatomic, strong) UILabel *titleLabel;

@end


// CustomHeaderView.m
#import "CustomHeaderView.h"

NSString *const CustomHeaderViewReuseIdentifier = @"CustomHeaderView";

@implementation CustomHeaderView

- (void)layoutSubviews {
    [super layoutSubviews];
    [self createSubViews];
}

- (void)createSubViews {
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor blackColor];
    CGFloat height = self.frame.size.height;
    _titleLabel.frame = CGRectMake(15, 0, 100, height);
    [self addSubview:_titleLabel];
}

@end

當(dāng)為以上代碼的時候,APP 在 iOS11 上運(yùn)行就會出現(xiàn)上圖的問題。
根據(jù) stackoverflow 的提示更改代碼之后,該問題便被修復(fù)。

以下為修復(fù)之后的CustomHeaderView類文件代碼

// CustomHeaderView.h

#import <UIKit/UIKit.h>

extern NSString *const CustomHeaderViewReuseIdentifier;

#ifdef __IPHONE_11_0
@interface CustomLayer : CALayer

@end
#endif

@interface CustomHeaderView : UICollectionReusableView

@property (nonatomic, strong) UILabel *titleLabel;

@end


// CustomHeaderView.m
#import "CustomHeaderView.h"

NSString *const CustomHeaderViewReuseIdentifier = @"CustomHeaderView";

#ifdef __IPHONE_11_0
@implementation CustomLayer

- (CGFloat) zPosition {
    return 0;
}

@end
#endif

@implementation CustomHeaderView

- (void)layoutSubviews {
    [super layoutSubviews];
    [self createSubViews];
}

- (void)createSubViews {
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.textColor = [UIColor blackColor];
    CGFloat height = self.frame.size.height;
    _titleLabel.frame = CGRectMake(15, 0, 100, height);
    [self addSubview:_titleLabel];
}

#ifdef __IPHONE_11_0
+ (Class)layerClass {
    return [CustomLayer class];
}
#endif

@end

以上代碼相對于之前有問題的代碼只是多了 #ifdef __IPHONE_11_0 ... #endif之間的內(nèi)容,使用 #ifdef __IPHONE_11_0 ... #endif母的是防止更改之后的代碼在 iOS 10 上出現(xiàn)問題,從而確保更改只是針對 iOS11 及之后的版本有效。

更改之后的效果圖如下所示:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容