我創建了一個LMLUpspringChoosePeriodView,它上面有一個tableView和一個backgroundView,我可以在我的項目中使用LMLUpspringChoosePeriodView作爲彈出窗口View。爲什麼我的tableView伸展了?
這是它的目錄:
的LMLUpspringChoosePeriodView有ChoosePeriodTableViewHeader和LMLUpspringPeriodCell,所有這些我創建了一個XIB:
,並在LMLUpspringPeriodCell,我添加了約束至名稱標籤,因此它必須位於LMLUpspringPeriodCell的中心:
但是但是,當我告訴了LMLUpspringChoosePeriodView爲上來看,總會有一個問題:
的的tableView似乎STRETCH時出的畫面。
我的tableView的約束是這樣的:
我的代碼如下:
LMLUpspringChoosePeriodView.h:
typedef void(^LMLUpspringBlock)();
#import <UIKit/UIKit.h>
@interface LMLUpspringChoosePeriodView : UIView
@property (weak, nonatomic) IBOutlet UIView *upspringBackView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottom_tableView;
@property (nonatomic, copy) LMLUpspringBlock block;
- (void)showSelf;
- (void)hideSelf;
@end
LMLUpspringChoosePeriodView.m:
#import "LMLUpspringChoosePeriodView.h"
#import "LMLUpspringPeriodCell.h"
#import "ChoosePeriodTableViewHeader.h"
@interface LMLUpspringChoosePeriodView() <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *width_tableView;
@property (nonatomic, strong) NSArray *title_arr;
@end
@implementation LMLUpspringChoosePeriodView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)awakeFromNib {
[super awakeFromNib];
//_width_tableView.constant = KWidth;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 4;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
ChoosePeriodTableViewHeader *header = [[NSBundle mainBundle] loadNibNamed:@"ChoosePeriodTableViewHeader" owner:self options:nil].firstObject;
header.cancel_block = ^() {
[self hideSelf];
};
return header;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
LMLUpspringPeriodCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LMLUpspringPeriodCell"];
if (cell == nil) {
cell = [[NSBundle mainBundle] loadNibNamed:@"LMLUpspringPeriodCell" owner:self options:nil].firstObject;
}
// 配置cell
cell.title_label.text = self.title_arr[indexPath.row];
if (indexPath.row == 3) {
cell.bottom_line.hidden = YES;
}
[self setNeedsLayout];
[self layoutIfNeeded];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 48;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
LMLUpspringPeriodCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *period_str = cell.title_label.text;
self.block(period_str);
[self hideSelf];
}
#pragma mark - action
- (void)showSelf {
self.hidden = NO;
[UIView animateWithDuration:0.5 animations:^{
_bottom_tableView.constant = -49;
_upspringBackView.alpha = 0.3f;
}];
}
- (void)hideSelf {
_bottom_tableView.constant = -_tableView.bounds.size.height - 49;
[UIView animateWithDuration:0.25 animations:^{
_upspringBackView.alpha = 0.f;
[self layoutIfNeeded];
} completion:^(BOOL finished) {
if (finished) {
self.hidden = YES;
}
}];
}
- (IBAction)tapBackView:(id)sender {
[self hideSelf];
}
#pragma mark - setter
-(NSArray *)title_arr {
if (!_title_arr) {
_title_arr = @[@"當天", @"最近一週", @"最近一個月", @"最近三個月"];
}
return _title_arr;
}
@end
的ChoosePeriodTableViewHeader.h:
#import <UIKit/UIKit.h>
typedef void(^CancelChooseUpspringView)();
@interface ChoosePeriodTableViewHeader : UIView
@property (nonatomic, copy) CancelChooseUpspringView cancel_block;
@end
的ChoosePeriodTableViewHeader.m:
#import "ChoosePeriodTableViewHeader.h"
@implementation ChoosePeriodTableViewHeader
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
// 取消
- (IBAction)cancel:(UIButton *)sender {
self.cancel_block();
}
的LMLUpspringPeriodCell.h:
#import <UIKit/UIKit.h>
typedef void(^ConfirmChoosePeriod)(NSString *);
@interface LMLUpspringPeriodCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *title_label;
@property (weak, nonatomic) IBOutlet UIView *bottom_line;
@property (nonatomic, copy) ConfirmChoosePeriod confirm_block;
@end
的LMLUpspringPeriodCell.m:
#import "LMLUpspringPeriodCell.h"
@implementation LMLUpspringPeriodCell
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
我曾嘗試使用在awakeFromNib
方法下面的代碼在LMLUpspringChoosePeriodView.m
(你可以看到LMLUpspringChoosePeriodView。米代碼,有在awakeFromNib方法註釋):
_width_tableView.constant = KWidth;
使用trailling
至約束所述的tableView的tableView
的替換,它仍然STRETCH時出來,但使用以下的代碼:
_width_tableView.constant = KWidth/2;
它是在iPhone7加上完美的iPhone7,但有問題:
所以,這是一個很奇怪的問題,該怎麼辦?
還不夠,還有第二期,最後一項無法點擊,不知是否與tabbar有關。
如何解決這兩個問題?提前致謝。
編輯-1
我添加LMLUpspringChoosePeriodView到kwyWindow在VC:
@property (nonatomic, strong) LMLUpspringChoosePeriodView *upspring_v;
....
[[UIApplication sharedApplication].keyWindow addSubview:self.upspring_v];
編輯-2
這是我PurchaseRecordVC .m,在那裏我展示LMLUpspringChoosePeriodView
:
#import "PurchaseRecordVC.h"
#import "LMLUpspringChoosePeriodView.h"
@interface PurchaseRecordVC()
@property (nonatomic, strong) LMLUpspringChoosePeriodView *upspring_v;
@end
@implementation PurchaseRecordVC
- (void)viewDidLoad {
[super viewDidLoad];
[self initData];
[self initUI];
}
#pragma mark - init
- (void)initData {
}
- (void)initUI {
// 添加界面
[[UIApplication sharedApplication].keyWindow addSubview:self.upspring_v];
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
- (IBAction)clickTheChoosePeriod:(UIButton *)sender {
[_upspring_v showSelf];
}
#pragma mark - getter
- (LMLUpspringChoosePeriodView *)upspring_v {
if (!_upspring_v) {
_upspring_v = [[NSBundle mainBundle] loadNibNamed:@"LMLUpspringChoosePeriodView" owner:self options:nil].firstObject;
// 初始化
_upspring_v.upspringBackView.alpha = 0;
_upspring_v.bottom_tableView.constant = -_upspring_v.tableView.bounds.size.height - 49;
_upspring_v.hidden = YES;
_upspring_v.block = ^(NSString *period){
};
}
return _upspring_v;
}
#pragma mark - dealloc
- (void)dealloc {
// 移除界面
[self.upspring_v removeFromSuperview];
}
@end
什麼這個問題可以詳細闡述一下嗎? – KKRocks
@KKRocks tableView似乎伸出了屏幕。 –
你可以添加截圖嗎? – KKRocks