亲自踩过的坑才是坑
在Dealloc使用weak造成的崩溃
从一个h5页面跳转到原生页面的时候,快速点击两次,在原生页面崩溃;
崩溃信息:
Cannot form weak reference to instance (0x7ffd73a0f200) of class ShareOrderMPVReplyViewController. It is possible that this object was over-released, or is in the process of deallocation.
后来定位到崩溃代码,是在一个懒加载里面,上代码:
- (void)dealloc{
self.mainTableView.dataSource = nil;
self.mainTableView.delegate = nil;
self.refreshHeaderView.delegate = nil;
self.inputTextView.delegate = nil;
self.editView.delegate = nil;
}
- (UITableView *)mainTableView {
if (!_mainTableView) {
_mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navigationController.navigationBar.bottom, SCREEN_WIDTH, self.inputView.top - self.navigationController.navigationBar.bottom) style:UITableViewStylePlain];
_mainTableView.backgroundColor =[UIColor colorWithHex:getAdjustCOLOR(@"#f6f6f6",DARK_LINE_BACK_COLOR_HEX)];
_mainTableView.delegate = self;
_mainTableView.dataSource = self;
_mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
SHAREORDER_FITIOS11(_mainTableView);
NSDictionary *serverConfig = [JDRouter openURL:@"router://AppManagerModule/getServerConfig"
arg:nil
error:nil
completion:nil];
NSInteger tipNumber = [[serverConfig objectForKey:@"refreshTipNum"] integerValue];
NewRefreshTableHeaderView *refreshHeaderView = [[NewRefreshTableHeaderView alloc] initWithFrame:CGRectMake(0.0f, 0.0f - _mainTableView.bounds.size.height, _mainTableView.bounds.size.width, _mainTableView.bounds.size.height) identifier:NSStringFromClass([self class]) tipNumber:tipNumber];
refreshHeaderView.delegate = self;
refreshHeaderView.scrollView = _mainTableView;
[_mainTableView addSubview:refreshHeaderView];
self.refreshHeaderView = refreshHeaderView;
}
return _mainTableView;
}
- (UIView *)inputView {
if (!_inputView) {
_inputView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - JDBIPhoneGlobalConfig.bottomSafeArea - 50, SCREEN_WIDTH, 50)];
_inputView.backgroundColor = [UIColor colorWithHex:getAdjustCOLOR(WHITE_COLOR_HEX,B2_COLOR_HEX)];
UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ONE_PIXEL)];
topLine.backgroundColor = [UIColor colorWithHex:getAdjustCOLOR(WHITE_COLOR_HEX,DARK_LINE_BACK_COLOR_HEX)];
[_inputView addSubview:topLine];
UILabel *inputLable = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 30, 30)];
inputLable.layer.masksToBounds = YES;
inputLable.backgroundColor = [UIColor colorWithHex:getAdjustCOLOR(@"#F2F2F2",B3_COLOR_HEX)];
inputLable.userInteractionEnabled = YES;
inputLable.layer.cornerRadius = 15;
inputLable.clipsToBounds = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(replyTap:)];
[inputLable addGestureRecognizer:tap];
[_inputView addSubview:inputLable];
CommentReplyInputTextView *inputTextView = [[CommentReplyInputTextView alloc]initWithFrame:CGRectMake(20, 10,SCREEN_WIDTH - 40, 30)];
inputTextView.placeholderColor = [UIColor colorWithHex:getAdjustCOLOR(@"#848484",C2_COLOR_HEX)];
inputTextView.font = [UIFont systemFontOfSize:12];
inputTextView.delegate = self;
inputTextView.backgroundColor = [UIColor clearColor];
inputTextView.scrollEnabled = NO;
inputTextView.textColor = [UIColor colorWithHex:getAdjustCOLOR(@"#2E2D2D",C1_COLOR_HEX)];
inputTextView.clearButtonMode = TextViewHasPenImage_withNoText;
inputTextView.placeholder = @"说点儿什么呗~";
[_inputView addSubview:inputTextView];
self.inputTextView = inputTextView;
}
return _inputView;
}
当快速点击进入该VC的时候,第一个实例化出来的vc在dealloc的时候,可能tableview 或者 inputTextView 还没有被实例化,那么,在dealloc中调用懒加载就会去创建并且设置代理、设置target,就会访问到weak。在dealloc里面访问weak就会造成崩溃
使用图片一定要添加contentMode
使用图片一定要添加contentMode。一定要添加contentMode。血的教训
在某banner列表页我没有使用contentMode属性,在过了两个版本才提出来,图片变形了。
contentMode 是UIView的属性,这个属性值决定了当视图的几何形状变化的时候如果复用它的内容。默认情况下,contentMode 是 UIViewContentModeScaleToFill的。
什么时候起作用?
视图frame或者bounds的高度发生变化的时候
赋给view的transform属性的值带有sacle
UIViewContentModeScaleToFill :改变内容的宽高比例,缩放内容,UIView中完整显示内容,填满UIView
UIViewContentModeScaleAspectFit:保持内容的宽高比,缩放内容,完整显示内容,最大化填充UIView,没填充上的区域透明
UIViewContentModeScaleAspectFill :保持内容宽高比,缩放内容,超出视图部分内容会被裁剪,填充UIView
UIViewContentModeRedraw:当View的bounds改变,系统会调用setNeedsDisplay,重新绘制视图
UIViewContentModeCenter:不缩放,内容在视图中间
UIViewContentModeTop
UIViewContentModeBottom
UIViewContentModeLeft
UIViewContentModeRight
UIViewContentModeTopLeft
UIViewContentModeTopRight
UIViewContentModeBottomLeft
UIViewContentModeBottomRight
借用一张图片放这里
–2020.7.23 更新—
UIViewContentModeScaleAspectFill :保持内容宽高比,缩放内容,超出视图部分内容会被裁剪,填充UIView
使用ScaleAspectFill 要配合使用bgImgview.clipsToBounds = YES ,不然超出屏幕范围的图片可能还会在展示