持续创造,加快成长!这是我参与「日新方案 10 月更文挑战」的第26天,点击查看活动概况
前语
- 下拉顶部布景色设置: 往tableView的父控件增加拉伸布景视图
- present 半屏适配
iOS13 modalPresentationStyle特点默许不是全屏款式
UIModalPresentationFullScreen
,而是半屏款式,需求依据需求手动设置。 present 半屏,会导致列表下拉改写失效。
I 下拉改写适配
1.1 下拉顶部布景色设置
- 设置下拉款式
#import <MJRefresh/MJRefresh.h>
@interface ERPMJRefreshNormalHeader4StyleWhite : MJRefreshNormalHeader
+ (instancetype)headerWithRefreshingTarget:(id)target refreshingAction:(SEL)action
{
MJRefreshHeader *cmp = [[self alloc] init];
[cmp setRefreshingTarget:target refreshingAction:action];
[self setupStyleWhite:cmp];
return cmp;
}
+(void)setupStyleWhite:(MJRefreshNormalHeader*)mj_header{
mj_header.stateLabel.textColor = UIColor.whiteColor;
mj_header.lastUpdatedTimeLabel.textColor=mj_header.stateLabel.textColor;
mj_header.loadingView.activityIndicatorViewStyle =UIActivityIndicatorViewStyleWhite;
}
- 下拉顶部布景色设置:往tableView的父控件增加拉伸布景视图
ERPMJRefreshNormalHeader4StyleWhite *mj_header =[ERPMJRefreshNormalHeader4StyleWhite headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
UIImage *bgImg = [UIImage getMaingradientColorImage];
UIColor *tmpColor = [UIColor colorWithPatternImage:bgImg];
mj_header.backgroundColor =tmpColor;
[self setupStretchViewColor:mj_header.backgroundColor tableView:_vcView.tableView];
_vcView.tableView.mj_header = mj_header;
_vcView.tableView.bounces = YES;
- (void)setupStretchViewColor:(UIColor*) stretchViewColor tableView:(UIView*)tableView{
[tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
self.stretchView.backgroundColor =stretchViewColor;
//往tableView的父控件增加拉伸布景视图
[tableView.superview addSubview:self.stretchView];
}
#pragma mark - 拉顶部布景色
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id>*)change context:(void*)context{
if([keyPath isEqualToString:@"contentOffset"]){
NSValue *value = change[NSKeyValueChangeNewKey];
CGFloat chaneoffsetY = value.UIOffsetValue.vertical;
UIView *mj_header= self.vcView.tableView.mj_header;
self.stretchView.frame = CGRectMake(0,0,mj_header.width,-chaneoffsetY-mj_header.height);
}
}
/**
拉伸布景
@return view
*/
-(UIView *)stretchView{
if (!_stretchView) {
UIView *tmp =[[UIView alloc] init];
_stretchView =tmp;
// _stretchView.backgroundColor = self.backgroundColor;
}
return _stretchView;
}
1.2 present 半屏适配
- 手动设置全屏款式
xxNavigationViewController *nav = [[xxNavigationViewController alloc] initWithRootViewController:loginViewController];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:nav animated:YES completion:nil];
//推荐运用UIModalPresentationOverFullScreen
- 灵敏操控模态展示的视图款式
iOS13适配【灵敏操控模态展示的视图款式】(全屏/下滑返回)文中供给完好demo源码
- 大局hook presentViewController办法
//
// UIViewController+ERPPresent13.h
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIViewController (ERPPresent13)
/**
Whether or not to set ModelPresentationStyle automatically for instance, Default is [Class K_automaticallySetModalPresentationStyle].
@return BOOL
*/
@property (nonatomic, assign) BOOL K_automaticallySetModalPresentationStyle;
/**
Whether or not to set ModelPresentationStyle automatically, Default is YES, but UIImagePickerController/UIAlertController is NO.
@return BOOL
*/
+ (BOOL)K_automaticallySetModalPresentationStyle;
@end
NS_ASSUME_NONNULL_END
//
// UIViewController+ERPPresent13.m
#import "UIViewController+ERPPresent13.h"
static const char *K_automaticallySetModalPresentationStyleKey;
@implementation UIViewController (ERPPresent13)
+ (void)load {
Method originAddObserverMethod = class_getInstanceMethod(self, @selector(presentViewController:animated:completion:));
Method swizzledAddObserverMethod = class_getInstanceMethod(self, @selector(K_presentViewController:animated:completion:));
method_exchangeImplementations(originAddObserverMethod, swizzledAddObserverMethod);
}
- (void)setK_automaticallySetModalPresentationStyle:(BOOL)K_automaticallySetModalPresentationStyle {
objc_setAssociatedObject(self, K_automaticallySetModalPresentationStyleKey, @(K_automaticallySetModalPresentationStyle), OBJC_ASSOCIATION_ASSIGN);
}
- (BOOL)K_automaticallySetModalPresentationStyle {
id obj = objc_getAssociatedObject(self, K_automaticallySetModalPresentationStyleKey);
if (obj) {
return [obj boolValue];
}
return [self.class K_automaticallySetModalPresentationStyle];
}
+ (BOOL)K_automaticallySetModalPresentationStyle {
if ([self isKindOfClass:[UIImagePickerController class]] || [self isKindOfClass:[UIAlertController class]]) {
return NO;
}
return YES;
}
- (void)K_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
if (@available(iOS 13.0, *)) {
if (viewControllerToPresent.K_automaticallySetModalPresentationStyle) {
viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
}
[self K_presentViewController:viewControllerToPresent animated:flag completion:completion];
} else {
// Fallback on earlier versions
[self K_presentViewController:viewControllerToPresent animated:flag completion:completion];
}
}
@end
II 上拉加载适配
2.1 安全间隔适配
blog.csdn.net/z929118967/…
问题:没有上拉的时候加载更多控件的案牍也显示出来了
修复方法1:修正视图间隔底部的高度
[self.vcView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.view).offset(0);
make.right.equalTo(weakSelf.view).offset(- 0);
make.top.equalTo(weakSelf.view).offset(0);
if(isHasSafeAreaInsets()){// 避免没有上拉的时候加载更多控件的案牍也显示出来了
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
CGFloat bottom = mainWindow.safeAreaInsets.bottom;
make.bottom.equalTo(weakSelf.view).offset(bottom);
}else{
make.bottom.equalTo(weakSelf.view);
}
}];
修复方法2:修正上拉加载控件间隔底部的高度 【推荐】
/** 疏忽多少scrollView的contentInset的bottom */
//@property (assign, nonatomic) CGFloat ignoredScrollViewContentInsetBottom;
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
CGFloat bottom = mainWindow.safeAreaInsets.bottom;//34
_tableView.mj_footer.ignoredScrollViewContentInsetBottom = isIphoneX ? bottom : 0;
判断安全区域间隔
static inline BOOL isIPhoneXSeries() {
if (@available(iOS 11.0, *)) {
UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
if (mainWindow.safeAreaInsets.bottom > 0.0) {
return YES;
}
}
return NO;
}
2.2 分页并发适配
方法1. 升级MJRefresh到3.7.5版别
Fix/duplicated async method -> Installing MJRefresh 3.7.5 (was 3.3.1)
- (void)executeRefreshingCallback
{
if (self.refreshingBlock) {
self.refreshingBlock();
}
if ([self.refreshingTarget respondsToSelector:self.refreshingAction]) {
MJRefreshMsgSend(MJRefreshMsgTarget(self.refreshingTarget), self.refreshingAction, self);
}
if (self.beginRefreshingCompletionBlock) {
self.beginRefreshingCompletionBlock();
}
}
方法2. 运用主动改写控件MJRefreshNormalHeader->MJRefreshAutoNormalFooter
see also
公号:iOS逆向