020606-04-聊天布局-键盘处理
2017-03-06 10:52
141 查看
// // XMGChatingViewController.h // 07-聊天布局 #import <UIKit/UIKit.h> @interface XMGChatingViewController : UIViewController @end
// // XMGChatingViewController.m // 07-聊天布局 #import "XMGChatingViewController.h" #import "XMGMessage.h" #import "XMGMessageCell.h" @interface XMGChatingViewController () <UITableViewDataSource, UITableViewDelegate> @property (nonatomic, strong) NSArray *messages; @property (weak, nonatomic) IBOutlet UITextField *messageField; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomSpacing; @end @implementation XMGChatingViewController - (NSArray *)messages { if (_messages == nil) { // 加载plist中的字典数组 NSString *path = [[NSBundle mainBundle] pathForResource:@"messages.plist" ofType:nil]; NSArray *dictArray = [NSArray arrayWithContentsOfFile:path]; // 字典数组 -> 模型数组 NSMutableArray *messageArray = [NSMutableArray array]; // 用来记录上一条消息模型 XMGMessage *lastMessage = nil; for (NSDictionary *dict in dictArray) { XMGMessage *message = [XMGMessage messageWithDict:dict]; message.hideTime = [message.time isEqualToString:lastMessage.time]; [messageArray addObject:message]; lastMessage = message; } _messages = messageArray; } return _messages; } - (void)viewDidLoad { [super viewDidLoad]; // 设置文本框左边的内容 UIView *leftView = [[UIView alloc] init]; leftView.frame = CGRectMake(0, 0, 10, 0); self.messageField.leftView = leftView; self.messageField.leftViewMode = UITextFieldViewModeAlways; // 监听键盘通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - 键盘处理 - (void)keyboardWillChangeFrame:(NSNotification *)note { // 取出键盘最终的frame CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; // 取出键盘弹出需要花费的时间 double duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; // 修改约束 self.bottomSpacing.constant = [UIScreen mainScreen].bounds.size.height - rect.origin.y; [UIView animateWithDuration:duration animations:^{ [self.view layoutIfNeeded]; }]; } #pragma mark - <UITableViewDataSource> - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.messages.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { XMGMessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"message"]; cell.message = self.messages[indexPath.row]; return cell; } #pragma mark - <UITableViewDelegate> - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath { return 200; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { XMGMessage *message = self.messages[indexPath.row]; return message.cellHeight; } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { // 退出键盘 // [self.messageField resignFirstResponder]; // [self.messageField endEditing:YES]; [self.view endEditing:YES]; } @end
相关文章推荐
- 020606-03-聊天布局-键盘处理
- 020606-06-聊天布局-键盘处理
- QQ聊天布局——利用通知机制实现键盘处理
- IOS聊天界面键盘处理
- Android键盘面板冲突 布局闪动处理方案
- Android键盘面板冲突 布局闪动处理方案
- Android-UI详解:软键盘弹出后,动态改变UI布局(IM聊天窗口设计示例)
- Android键盘面板冲突 布局闪动处理方案
- Android键盘面板冲突 布局闪动处理方案
- 解决Android键盘面板冲突 布局闪动处理方案
- Android键盘面板冲突 布局闪动处理方案
- 【iOS开发-65】QQ聊天界面案例:自定义cell、图片拉伸处理、NSNotification通知、键盘与视图移动以及输入框左边缩进处理
- 猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- (素材源码)猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- android 类似QQ底部输入框弹出键盘和面板冲突 布局闪动处理方案(转)
- Android键盘面板冲突 布局闪动处理方案
- 黑马程序员--10.网络编程--04.【UDP_键盘录入】【UDP_聊天】
- (素材源码)猫猫学IOS(十八)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- iOS UI基础-10.0 QQ聊天布局之键盘及文本使用
- 020606-07-聊天布局