使用iOS SDK:
我有一个UIView带有UITextField键盘的s.我需要它能够:
UIScrollView一旦键盘出现,允许滚动内容以查看其他文本字段
自动"跳跃"(通过向上滚动)或缩短
我知道我需要一个UIScrollView.我已经尝试将我的类更改UIView为a UIScrollView但我仍然无法向上或向下滚动文本框.
我需要a UIView和a UIScrollView吗?一个进去另一个吗?
需要实现什么才能自动滚动到活动文本字段?
理想情况下,尽可能多的组件设置将在Interface Builder中完成.我只想编写需要它的代码.
注意:我正在使用的UIView(或UIScrollView)是由tabbar(UITabBar)启动的,它需要正常运行.
编辑:我正在添加滚动条,仅用于键盘出现时.即使它不需要,我觉得它提供了更好的界面,因为用户可以滚动和更改文本框.
我已经让它工作,我改变UIScrollView了键盘上下的框架大小.我只是使用:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
//Keyboard becomes visible
scrollView.frame = CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
scrollView.frame.size.height - 215 + 50); //resize
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
//keyboard will hide
scrollView.frame = CGRectMake(scrollView.frame.origin.x,
scrollView.frame.origin.y,
scrollView.frame.size.width,
scrollView.frame.size.height + 215 - 50); //resize
}
Run Code Online (Sandbox Code Playgroud)
但是,这不会自动"向上移动"或将可见区域中的下部文本字段居中,这是我真正想要的.
我在一周前提交了我的应用程序,今天收到了可怕的拒绝电子邮件.它告诉我我的应用程序无法被接受,因为我使用的是非公共API; 具体来说,它说,
应用程序中包含的非公共API是firstResponder.
现在,违规的API调用实际上是我在SO上找到的解决方案:
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *firstResponder = [keyWindow performSelector:@selector(firstResponder)];
Run Code Online (Sandbox Code Playgroud)
如何在屏幕上显示当前的第一响应者?我正在寻找一种不会让我的应用被拒绝的方法.
我正在使用Swift进行iOS编程,我正在使用此代码移动UITextField,但它不起作用.我keyboardWillShow正确调用该函数,但文本字段不移动.我正在使用autolayout.
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name:UIKeyboardWillShowNotification, object: nil);
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name:UIKeyboardWillHideNotification, object: nil);
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self);
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
//let contentInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardSize.height, right: 0)
var frame = self.ChatField.frame
frame.origin.y = frame.origin.y - keyboardSize.height + 167
self.chatField.frame = frame
println("asdasd")
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为iPhone构建输入屏幕.屏幕有许多输入字段.其中大多数位于屏幕顶部,但两个字段位于底部.当用户尝试编辑屏幕底部的文本时,键盘将弹出并覆盖屏幕.我找到了一个简单的解决方案,可以在发生这种情况时将屏幕向上移动,但结果是屏幕总是向上移动,当用户尝试编辑屏幕时,屏幕顶部的字段会移动.
有没有办法让屏幕只在编辑底部字段时移动?
我使用了这里找到的代码:
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWillShow(sender: NSNotification) {
self.view.frame.origin.y -= 150
}
func keyboardWillHide(sender: NSNotification) {
self.view.frame.origin.y += 150
}
Run Code Online (Sandbox Code Playgroud) 
UITextView当需要一些输入时,我需要将键盘移开。我正在使用以下代码,它与 a 完美配合,UITextField但与 a 则根本不起作用UITextView。
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(DailyNotesViewController.keyboardWillShow(_:)),
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(DailyNotesViewController.keyboardWillHide(_:)),
name: UIKeyboardWillHideNotification,
object: nil)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
func adjustInsetForKeyboardShow(show: Bool, notification: NSNotification) {
let userInfo = notification.userInfo ?? [:]
let keyboardFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue()
let adjustmentHeight = (CGRectGetHeight(keyboardFrame) + 20) * (show ? 1 : -1)
scrollView.contentInset.bottom += adjustmentHeight
scrollView.scrollIndicatorInsets.bottom += adjustmentHeight
}
func keyboardWillShow(notification: NSNotification) {
adjustInsetForKeyboardShow(true, notification: notification)
}
func …Run Code Online (Sandbox Code Playgroud) ios ×5
swift ×4
cocoa-touch ×2
keyboard ×2
objective-c ×2
uitextfield ×2
uikeyboard ×1
uiscrollview ×1
uitextview ×1
xcode6 ×1