在开发过程中,个人喜欢自定制view,比较偏爱xib,常见的用法是先创建一个view,比如ZQPayReultHeaderView,然后创建一个同名的xib,然后将两者关联
ZQPayReultHeaderView.h
#import <UIKit/UIKit.h>
@interface ZQPayReultHeaderView : UIView
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
@property (weak, nonatomic) IBOutlet UIImageView *resultImageView;
@property (assign, nonatomic) BOOL isSuccess;
+ (instancetype) resultView;
@end
ZQPayReultHeaderView.m
#import "ZQPayReultHeaderView.h"
@implementation ZQPayReultHeaderView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
+ (instancetype) resultView{
return [[NSBundle mainBundle] loadNibNamed:@"ZQPayReultHeaderView" owner:nil options:nil].firstObject;
}
- (void)setIsSuccess:(BOOL)isSuccess{
_isSuccess = isSuccess;
if (isSuccess) {
self.resultImageView.image = [UIImage imageNamed:@"paySuccess"];
self.resultLabel.text = @"支付成功!";
}else{
self.resultImageView.image = [UIImage imageNamed:@"fail"];
self.resultLabel.text = @"支付失败!";
}
}
@end
还有一个ZQPayResultDetailView的view,这个和ZQPayReultHeaderView类似,就不加代码了
ZQPayResultViewController.m
#import "ZQPayResultViewController.h"
#import "ZQPayResultDetailView.h"
#import "ZQPayReultHeaderView.h"
@interface ZQPayResultViewController ()
@property (nonatomic,strong)ZQPayReultHeaderView *resultView;
@property (nonatomic,strong)ZQPayResultDetailView *resultDetailView;
@end
@implementation ZQPayResultViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addTitleViewWithName:@"支付结果"];
[self.view addSubview:self.resultView];
if (self.isSuccess) {
[self.view addSubview:self.resultDetailView];
}
}
- (ZQPayReultHeaderView *)resultView{
if (!_resultView) {
_resultView = [ZQPayReultHeaderView resultView];
_resultView.isSuccess = self.isSuccess;
_resultView.frame = CGRectMake(0, 0, kScreenSize.width, 200);
}
return _resultView;
}
- (ZQPayResultDetailView *)resultDetailView{
if (!_resultDetailView) {
_resultDetailView = [ZQPayResultDetailView sharedInstance];
_resultDetailView.frame = CGRectMake(0, CGRectGetMaxY(self.resultView.frame), kScreenSize.width, 140);
}
return _resultDetailView;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
最后的结果竟然是。。。
暂时找到的解决办法是在controller的view上又加了一个mainView,然后又将这两个view加上去,代码为
#import "ZQPayResultViewController.h"
#import "ZQPayResultDetailView.h"
#import "ZQPayReultHeaderView.h"
@interface ZQPayResultViewController ()
@property (nonatomic,strong)ZQPayReultHeaderView *resultView;
@property (nonatomic,strong)ZQPayResultDetailView *resultDetailView;
@end
@implementation ZQPayResultViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self addTitleViewWithName:@"支付结果"];
UIView *mainView = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:mainView];
[mainView addSubview:self.resultView];
if (self.isSuccess) {
[mainView addSubview:self.resultDetailView];
}
}
- (ZQPayReultHeaderView *)resultView{
if (!_resultView) {
_resultView = [ZQPayReultHeaderView resultView];
_resultView.isSuccess = self.isSuccess;
_resultView.frame = CGRectMake(0, 0, kScreenSize.width, 200);
}
return _resultView;
}
- (ZQPayResultDetailView *)resultDetailView{
if (!_resultDetailView) {
_resultDetailView = [ZQPayResultDetailView sharedInstance];
_resultDetailView.frame = CGRectMake(0, CGRectGetMaxY(self.resultView.frame), kScreenSize.width, 140);
}
return _resultDetailView;
}
这个时候结果就正常了
所以,为什么自定义的view直接加在controller的底层view就会出现这个问题呢?本人菜鸟,求大神指教!