我正在努力与似乎应该是简单的事情,但结果不是。我想添加一个UITextField
并将发送按钮固定到我的标签栏上方的屏幕底部,并在我的'UITableview'下方。我尝试了一个页脚,但它只是增加了最后一行的底部。添加像典型的聊天应用程序的textfield和发送按钮
当键盘出现时,我需要它向上移动。
我正在努力与似乎应该是简单的事情,但结果不是。我想添加一个UITextField
并将发送按钮固定到我的标签栏上方的屏幕底部,并在我的'UITableview'下方。我尝试了一个页脚,但它只是增加了最后一行的底部。添加像典型的聊天应用程序的textfield和发送按钮
当键盘出现时,我需要它向上移动。
您可以在自定义UIViewController中插入tableView,并在底部添加一个带有发送UITextField的子视图。然后,按照您的意愿按照关于键盘演示的通知和更改tableView并发送UITextField帧。类似的东西:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:(BOOL)animated];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillAppear:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillDisappear:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:(BOOL)animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - Keyboard appearance/disappearance handling
- (void)keyboardWillAppear:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
[self.tableView setContentInset:contentInsets];
[self.tableView setScrollIndicatorInsets:contentInsets];
CGRect messageFrame = self.messageTextView.frame;
messageFrame.origin.y -= keyboardSize.height;
[self.messageTextView setFrame:messageFrame];
}
- (void)keyboardWillDisappear:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.25];
[self.tableView setContentInset:UIEdgeInsetsZero];
[UIView commitAnimations];
[self.tableView setScrollIndicatorInsets:UIEdgeInsetsZero];
CGRect messageFrame = self.messageTextView.frame;
messageFrame.origin.y += keyboardSize.height;
[self.messageTextView setFrame:messageFrame];
}
谢谢你。所以看起来这代表了键盘处理。在故事板中,我将屏幕设置为一个带有tableview的UIViewController。我从底部缩小桌面并添加了一个视图,其中包含文本字段和按钮...但是当我运行应用程序时...视图,按钮和文本框全部不显示。 – mreynol
有很多可能的方法你做错了什么:)你确定正确的UIViewController加载?尝试在-viewDidLoad方法中记录一些消息。 – Wisors
是的,它绝对加载我的看法。我的tableview正在满足我的期望。当我在故事板缩小桌面视图时,这也反映出来。但是,我已添加到我的tableview下面的视图以及textfield和button不显示。 – mreynol
斯威夫特:
var commentField:UITableViewCell!
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector:"keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(true)
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func keyboardWillAppear(notification: NSNotification){
var userInfo:NSDictionary = notification.userInfo!
var keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size
var contentInsets:UIEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0)
self.tableView.contentInset = contentInsets
self.tableView.scrollIndicatorInsets = contentInsets
var messageFrame:CGRect = self.commentField.frame
messageFrame.origin.y -= keyboardSize.height
self.commentField.frame = messageFrame
}
func keyboardWillDisappear(notification: NSNotification){
var userInfo:NSDictionary = notification.userInfo!
var keyboardSize:CGSize = userInfo.objectForKey(UIKeyboardFrameBeginUserInfoKey)!.CGRectValue().size
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0.25)
self.tableView.contentInset = UIEdgeInsetsZero
UIView.commitAnimations()
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
var messageFrame:CGRect = self.commentField.frame
messageFrame.origin.y += keyboardSize.height
self.commentField.frame = messageFrame
}
你能解释一下,为什么commentField是tableViewCell? –
不重要,只是碰巧在我的情况 – Esqarrouth
你能帮我一点吗?我有tableView和uiview的文本框和按钮。这个视图对tabbar 0有底部约束。我是否应该增加这个约束来使它工作?谢谢。我会uparrow,如果它有帮助:) –
你可以给你的问题 – AFB
的更多细节,为什么我才拿到停靠? – mreynol
凯文,我只是想添加一个视图到我的tableview的底部,其中包含一个文本框和按钮就像一个典型的聊天应用程序。当我在文本框内点击时,键盘会呈现出来,并用textfield将视图推到键盘上方。我曾尝试添加一个不支持我想要的结果的页脚,并且我尝试添加一个视图但没有运气。 – mreynol