在開發(fā)過程中算利,個(gè)人喜歡自定制view污筷,比較偏愛xib,常見的用法是先創(chuàng)建一個(gè)view桥氏,比如ZQPayReultHeaderView温峭,然后創(chuàng)建一個(gè)同名的xib,然后將兩者關(guān)聯(lián)
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
還有一個(gè)ZQPayResultDetailView的view字支,這個(gè)和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:@"支付結(jié)果"];
[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.
}
最后的結(jié)果竟然是。堕伪。揖庄。
暫時(shí)找到的解決辦法是在controller的view上又加了一個(gè)mainView,然后又將這兩個(gè)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:@"支付結(jié)果"];
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;
}
這個(gè)時(shí)候結(jié)果就正常了
所以蹄梢,為什么自定義的view直接加在controller的底層view就會(huì)出現(xiàn)這個(gè)問題呢?本人菜鳥富俄,求大神指教禁炒!