//
// SignStepRemindView.m
// LTPMS
//
// Created by longdw on 16/7/14.
// Copyright © 2016年 longdw. All rights reserved.
//
#define kPadding 10
#import "SignStepRemindView.h"
#import "SignStepRemindCell.h"
#import "UITableViewCell+AutoHeightForCell.h"
#import "NSString+Frame.h"
@interface SignStepRemindView() <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
@property(nonatomic, strong) UIWindow *window;
@property(nonatomic, strong) UIView *backIv;
@property(nonatomic, weak) UIView *contentView;
@property(nonatomic, weak) UITableView *tableView;
@property(nonatomic, weak) UIScrollView *scrollView;
@property(nonatomic, weak) UIView *remindContentView;
@property(nonatomic, weak) UIButton *leftBtn;
@property(nonatomic, weak) UIButton *rightBtn;
@property(nonatomic, weak) UILabel *titleLabel;
@property(nonatomic, assign) NSInteger step;
@property(nonatomic, weak) UITextField *textField;
@end
@implementation SignStepRemindView
- (instancetype)init
{
if (self = [super init]) {
self.backgroundColor = kGlobalBgColor;
[self addViews];
}
return self;
}
- (void)addViews
{
self.layer.cornerRadius = 4;
self.layer.masksToBounds = YES;
UILabel *titleLabel = [[UILabel alloc] init];
self.titleLabel = titleLabel;
titleLabel.text = @"select step";
titleLabel.textColor = kNavColor;
titleLabel.font = [UIFont systemFontOfSize:16];
[self addSubview:titleLabel];
WEAKSELF
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.left.equalTo(weakSelf).offset(kPadding);
make.height.mas_equalTo(30);
}];
UIView *bottomView = [[UIView alloc] init];
[self addSubview:bottomView];
[bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
make.bottom.equalTo(weakSelf).offset(-kPadding * 0.5);
make.left.equalTo(weakSelf);
make.right.equalTo(weakSelf).offset(-kPadding);
make.height.mas_equalTo(40);
}];
UIButton *rightBtn = [[UIButton alloc] init];
self.rightBtn = rightBtn;
rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[rightBtn setTitleColor:kNavColor forState:UIControlStateNormal];
[rightBtn setTitle:@"next" forState:UIControlStateNormal];
[rightBtn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:rightBtn];
[rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(bottomView);
make.height.equalTo(bottomView);
make.centerY.equalTo(bottomView.mas_centerY);
make.width.mas_equalTo(60);
}];
UIButton *leftBtn = [[UIButton alloc] init];
self.leftBtn = leftBtn;
leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:17];
[leftBtn setTitleColor:kNavColor forState:UIControlStateNormal];
[leftBtn setTitle:@"cancel" forState:UIControlStateNormal];
[leftBtn addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
[bottomView addSubview:leftBtn];
[leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(rightBtn.mas_left).offset(-kPadding);
make.height.equalTo(bottomView);
make.centerY.equalTo(bottomView.mas_centerY);
make.width.mas_equalTo(60);
}];
UIScrollView *scrollView = [[UIScrollView alloc] init];
self.scrollView = scrollView;
scrollView.delegate = self;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
[self addSubview:scrollView];
[scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(titleLabel.mas_bottom).offset(kPadding);
make.bottom.equalTo(bottomView.mas_top);
make.left.equalTo(weakSelf).offset(kPadding);
make.right.equalTo(weakSelf).offset(-kPadding);
}];
UIView *contentView = [[UIView alloc] init];
self.contentView = contentView;
[self.scrollView addSubview:contentView];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(scrollView);
make.height.equalTo(scrollView);
}];
//first
UITableView *tableView = [[UITableView alloc] init];
tableView.backgroundColor = [UIColor redColor];
self.tableView = tableView;
// tableView.delegate = self;
// tableView.dataSource = self;
UIView *view = [UIView new];
view.backgroundColor = [UIColor clearColor];
[tableView setTableFooterView:view];
[contentView addSubview:tableView];
[tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.equalTo(contentView);
make.width.equalTo(weakSelf.scrollView);
}];
//second
UIScrollView *remindScrollView = [[UIScrollView alloc] init];
[contentView addSubview:remindScrollView];
[remindScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(tableView.mas_right);
make.top.bottom.equalTo(contentView);
make.width.equalTo(weakSelf.scrollView);
}];
UIView *remindContentView = [[UIView alloc] init];
self.remindContentView = remindContentView;
[remindScrollView addSubview:remindContentView];
[remindContentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(remindScrollView);
make.width.equalTo(remindScrollView);
}];
[contentView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(remindScrollView.mas_right);
}];
[self addRemindView];
}
- (void)addRemindView
{
UIView *editView = [[UIView alloc] init];
editView.userInteractionEnabled = YES;
[self.remindContentView addSubview:editView];
WEAKSELF
[editView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(weakSelf.remindContentView).offset(kPadding);
make.right.equalTo(weakSelf.remindContentView).offset(-kPadding);
make.height.mas_equalTo(40);
}];
NSString *title = @"content:";
UIFont *font = [UIFont systemFontOfSize:15];
CGFloat width = [title boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) attributes:@{ NSFontAttributeName : font }].width;
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = font;
titleLabel.text = title;
[editView addSubview:titleLabel];
UITextField *textField = [[UITextField alloc] init];
self.textField = textField;
[self.textField setEnabled:YES];
// [textField becomeFirstResponder];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.backgroundColor = [UIColor whiteColor];
[editView addSubview:textField];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(editView);
make.centerY.equalTo(editView.mas_centerY);
make.width.mas_equalTo(width);
}];
[textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(titleLabel.mas_right);
make.top.bottom.equalTo(editView);
make.centerY.equalTo(editView.mas_centerY);
make.right.equalTo(editView);
}];
}
#pragma mark - UITableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (SignStepRemindCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = @"Cell";
SignStepRemindCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (!cell) {
cell = [[SignStepRemindCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [UITableViewCell cellHeightForIndexPath:indexPath tableView:tableView] + 10;
}
- (void)show:(UIView *)view
{
// UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIWindow *window = view.window;
// self.window = window;
// window.windowLevel = UIWindowLevelNormal;
// window.backgroundColor = [UIColor clearColor];
// window.alpha = 1;
// window.hidden = NO;
[window makeKeyAndVisible];
NSLog(@"key1-->%@, key2-->%@", [UIApplication sharedApplication].keyWindow, window);
UIView *backIv = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.backIv = backIv;
backIv.backgroundColor = [UIColor blackColor];
backIv.alpha = 0.7;
[window addSubview:backIv];
[window addSubview:self];
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * 0.7;
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(30);
make.right.mas_equalTo(-30);
make.height.mas_equalTo(screenHeight);
make.center.equalTo(window);
}];
}
- (void)layoutSubviews
{
[super layoutSubviews];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (void)dismiss
{
[self.backIv removeFromSuperview];
[self removeFromSuperview];
}
#pragma mark - 滚动代理
#pragma mark scrollview减速完毕就会调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
//获取当前页码
int pageNo = scrollView.contentOffset.x / scrollView.frame.size.width;
if (pageNo == 0) {
[self.leftBtn setTitle:@"cancel" forState:UIControlStateNormal];
[self.rightBtn setTitle:@"next" forState:UIControlStateNormal];
self.titleLabel.text = @"select step";
self.step = 0;
} else {
[self.leftBtn setTitle:@"pre" forState:UIControlStateNormal];
[self.rightBtn setTitle:@"finish" forState:UIControlStateNormal];
self.titleLabel.text = @"select person";
self.step = 1;
}
}
- (void)cancel
{
if (self.step == 0) {//cancel
[self dismiss];
} else {//pre
self.leftBtn.enabled = NO;
self.rightBtn.enabled = NO;
CGPoint offset = self.scrollView.contentOffset;
offset.x -= self.scrollView.frame.size.width;
[UIView animateWithDuration:0.25 animations:^{
self.scrollView.contentOffset = offset;
} completion:^(BOOL finished) {
[self.leftBtn setTitle:@"cancel" forState:UIControlStateNormal];
[self.rightBtn setTitle:@"next" forState:UIControlStateNormal];
self.titleLabel.text = @"select step";
self.step = 0;
self.leftBtn.enabled = YES;
self.rightBtn.enabled = YES;
}];
}
}
- (void)next
{
if (self.step == 0) {//next
self.leftBtn.enabled = NO;
self.rightBtn.enabled = NO;
CGPoint offset = self.scrollView.contentOffset;
offset.x += self.scrollView.frame.size.width;
[UIView animateWithDuration:0.25 animations:^{
self.scrollView.contentOffset = offset;
} completion:^(BOOL finished) {
[self.leftBtn setTitle:@"pre" forState:UIControlStateNormal];
[self.rightBtn setTitle:@"finish" forState:UIControlStateNormal];
self.titleLabel.text = @"select person";
self.step = 1;
self.leftBtn.enabled = YES;
self.rightBtn.enabled = YES;
}];
} else {//finish
}
}
- (void)dealloc
{
// NSArray *subViews = [self.window subviews];
// for (id obj in subViews) {
// [obj removeFromSuperview];
// }
// [self.window resignKeyWindow];
// [self.window removeFromSuperview];
// self.window = nil;
}
@end
这是自定义单元格
#define kPadding 10
#import "SignStepRemindCell.h"
@interface SignStepRemindCell()
@property(nonatomic, strong) UIImage *unSelectedImage;
@property(nonatomic, strong) UIImage *selectedImage;
@end
@implementation SignStepRemindCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.unSelectedImage = [UIImage imageNamed:@"contact_icon_checkbox"];
self.selectedImage = [UIImage imageNamed:@"contact_icon_checkbox_selected_all"];
[self addViews];
}
return self;
}
- (void)addViews
{
UIImageView *checkImageView = [[UIImageView alloc] initWithImage:self.selectedImage];
[self.contentView addSubview:checkImageView];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.textColor = [UIColor blackColor];
titleLabel.font = [UIFont systemFontOfSize:15];
[self.contentView addSubview:titleLabel];
UILabel *subTitleLabel = [[UILabel alloc] init];
subTitleLabel.lineBreakMode = NSLineBreakByWordWrapping;
subTitleLabel.numberOfLines = 0;
subTitleLabel.textColor = [UIColor blackColor];
subTitleLabel.font = [UIFont systemFontOfSize:15];
[self.contentView addSubview:subTitleLabel];
WEAKSELF
[checkImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(weakSelf.contentView).offset(kPadding);
make.centerY.equalTo(weakSelf.contentView.mas_centerY);
make.size.mas_equalTo(weakSelf.selectedImage.size);
}];
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(weakSelf.contentView).offset(kPadding);
make.left.equalTo(checkImageView.mas_right).offset(kPadding);
make.right.equalTo(weakSelf.contentView).offset(-kPadding);
make.height.mas_greaterThanOrEqualTo(0);
}];
[subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(titleLabel.mas_bottom).offset(5);
make.left.equalTo(titleLabel.mas_left);
make.right.equalTo(weakSelf.contentView).offset(-kPadding);
make.height.mas_greaterThanOrEqualTo(0);
}];
titleLabel.text = @"num:2";
NSString *text = @"<strong>test:</strong>test1, test2, test3, test4";
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[text dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
subTitleLabel.attributedText = attrStr;
}
@end
我在vc中使用此自定义视图
SignStepRemindView *stepRemindView = [[SignStepRemindView alloc] init];
[stepRemindView show:self.view];
我的问题是UITextField无法编辑和触摸,键盘从未显示过,但当我使用[textField becomeFirstResponder]时,我可以输入,键盘显示,但光标不显示。 非常感谢你。
答案 0 :(得分:0)
我解决了它。原因是约束不完整。 在方法addRemindView
的末尾添加以下代码static