我有textboxes
。当我点击它时,键盘弹出。但是,由于uitextfield
位于下方的某个位置,因此当键盘弹出时会被覆盖。 (键盘显示在文本字段上方,因此用户无法看到它。)
我需要一种方法来移动textfield
,以便用户可以看到它。 iPhone facebook
应用有一个解决方法,他们缩小视图上的UIComponents
以在顶部显示textfield
,因此它不会从键盘覆盖。
有人可以指点我开始使用教程或示例代码吗?
答案 0 :(得分:5)
选项-1:您可以参考以下链接中已接受为答案的此代码:
How to make a UITextField move up when keyboard is present?
这肯定会帮助您并为您提供所需的解决方案。
选项-2:此处还有一个现成的指南:
http://www.edumobile.org/iphone/iphone-programming-tutorials/scrollview-example-in-iphone/
我认为你肯定会得到这个帮助
选项-3:您可以参考:
http://www.iphonesampleapps.com/2009/12/adjust-uitextfield-hidden-behind-keyboard-with-uiscrollview/
选项-4: Apple文档:
希望这会对你有所帮助。
答案 1 :(得分:2)
在iOS9上处理空间情况,例如预测文本打开和关闭以及不同大小的键盘。
var defaultFrame: CGRect!
defaultFrame = self.frame
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyBoardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
func moveViewWithKeyboard(height: CGFloat) {
self.frame = CGRectOffset(defaultFrame, 0, height)
}
func keyBoardWillShow(notification: NSNotification) {
let frame = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
moveViewWithKeyboard(-frame.height)
}
func keyBoardWillHide(notification: NSNotification) {
moveViewWithKeyboard(0)
}
答案 2 :(得分:1)
最好的方法是使用UIScrollView
并在需要开始编辑文本时将其移动。
这个问题/答案将为您提供设置的粗略指南
答案 3 :(得分:0)
它可能对你有帮助... bottomLayoutConstrain是来自底层的约束 文本字段从您想要的位置到视图的底部。斯威夫特代码:
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShowNotification:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHideNotification:", name: UIKeyboardWillHideNotification, object: nil)
func keyboardWillShowNotification(notification:NSNotification){
let keyboardInfo:NSDictionary = notification.userInfo!
let keyboardFrameBegin:AnyObject = keyboardInfo.valueForKey(UIKeyboardFrameEndUserInfoKey)!
keyboardFrameBeginRect = keyboardFrameBegin.CGRectValue
self.view.layoutIfNeeded()
self.bottomLayoutConstrain.constant = self.keyboardFrameBeginRect!.height
self.view.layoutIfNeeded()
}
func keyboardWillHideNotification(notification:NSNotification){
self.view.layoutIfNeeded()
bottomLayoutConstrain?.constant = 0
self.view.layoutIfNeeded()
}
deinit {
// perform the deinitialization
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
// NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
目标c代码
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShowNotification:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHideNotification:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void) keyboardWillShowNotification:(NSNotification *) notification{
NSDictionary *keyboardInfo = notification.userInfo;
keyboardFrameBeginRect = [keyboardInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[self.view layoutIfNeeded];
// [UIView animateWithDuration:1.0 animations:^{
topLayoutConstraints.constant = -keyboardFrameBeginRect.size.height;
bottomLayoutConstrain.constant = keyboardFrameBeginRect.size.height;
// }];
[self.view layoutIfNeeded];
}
-(void) keyboardWillHideNotification:(NSNotification *) notification{
[self.view layoutIfNeeded];
topLayoutConstraints.constant = 0;
bottomLayoutConstrain.constant = 0;
[self.view layoutIfNeeded];
}