我创建了一个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