![](/img/trans.png)
[英]UIPageViewController bug with UITextField and keyboard showing
[英]UITextfield showing and dismissing very quickly in UIPageViewController (Swift)
我有一個UIPageViewController
作為我的應用程序的演練演示。 演練基本上總共有 5 頁。 第 4 頁有一個UITextField
編程方式設置。 不要介意標簽或圖像等其他對象。
我遇到的問題是,當用戶到達第 4 頁時,用戶會看到一個文本字段,我希望鍵盤通過becomeFirstResponder
自動顯示。
當我使用文本字段加載第 4 個視圖時,它會很快地顯示在模擬器和我的設備上,然后又返回。 它基本上通過becomeFirstResponder
代碼出現,但不知何故在 1 秒內becomeFirstResponder
。
我已經以編程方式並通過故事板UITextfield
嘗試了此UITextfield
以查看是否有所不同,但兩者似乎都不起作用。
執行標簽的代碼也是我以編程方式執行UITextField
但在本示例中,我將其刪除並使用了 IBOutlet,如您所見。
另外,在這種case 3
我會使用becomeFirstResponder
來激活鍵盤,但它仍然無法按預期工作。
現在我把它留在了viewWillAppear
方法中,如你所見。
這是我針對此特定場景的代碼:
import UIKit
class WalkthroughViewController: UIViewController {
@IBOutlet var headingLabel:UILabel!
@IBOutlet var contentLabel:UILabel!
@IBOutlet var contentImageView:UIImageView!
@IBOutlet var pageControl:UIPageControl!
@IBOutlet var forwardButton:UIButton!
// This was added via storyboard via drag and drop
@IBOutlet var nameField: UITextField!
// This is a UITextfield programmatically
let textField = UITextField(frame: CGRectMake(20, 200.0, 330.0, 40.0)) // x=x-cord, y=y-cord, Width, Height
// May 2 Updates
func textFieldFunc() {
textField.textAlignment = NSTextAlignment.Center
textField.textColor = UIColor.wetAsphaltColor()
textField.backgroundColor = UIColor.whiteColor()
textField.font = UIFont(name: "avenir", size: 21)
textField.borderStyle = UITextBorderStyle.None
textField.autocapitalizationType = UITextAutocapitalizationType.Words // If you need any capitalization
textField.becomeFirstResponder()
self.view.addSubview(textField)
}
func nameLabel() {
let label = UILabel(frame: CGRectMake(15, 180, 265, 33))
label.center = CGPointMake(185, 160)
label.textColor = UIColor.cloudsColor()
label.font = UIFont(name: "avenir", size: 30)
label.textAlignment = NSTextAlignment.Center
label.text = "Whats your name?"
self.view.addSubview(label)
}
@IBAction func submit(sender: AnyObject) {
// Thisis going to handle the name later and it will then reguster the user for later use within the app
}
var index = 0
var heading = ""
var imageFile = ""
var content = ""
override func viewDidLoad() {
super.viewDidLoad()
headingLabel.text = heading
contentLabel.text = content
contentImageView.image = UIImage(named: imageFile)
pageControl.currentPage = index
// 0...2
switch index {
case 0...2: forwardButton.setTitle("NEXT", forState: UIControlState.Normal)
nameField.hidden = true
// 3
case 3:
nameLabel()
nameField.hidden = false
case 4:
forwardButton.setTitle("DONE", forState: UIControlState.Normal)
default: break
}
print("Index: \(index)")
}
@IBAction func nextButtonTapped(sender: UIButton) {
switch index {
case 0...3: // 2
let pageViewController = parentViewController as! WalkthroughPageViewController
pageViewController.forward(index)
case 4: // 3
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(true, forKey: "finishedWalkedThrough")
dismissViewControllerAnimated(true, completion: nil)
default: break
}
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if nameField.hidden == false {
nameField.becomeFirstResponder()
}
}
}
我的問題是,如何解決這個問題,以便當第 4 頁以滑動方式呈現時, UITextfield
可以像普通視圖一樣呈現鍵盤?
正如我在一個非常相似的帖子中看到的,這確實有效。 我設法通過使用它來解決它:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if index == 3 {
dispatch_async(dispatch_get_main_queue(), {() -> Void in
let strongSelf: WalkthroughViewController = self
strongSelf.textField.becomeFirstResponder()
})
}
}
現在,當我滾動到第 4 頁(從 0 開始的第 3 頁)時,它會像普通UITextField
一樣加載帶有.becomeFirstResponder
的鍵盤。
dispatch_after(DISPATCH_TIME_NOW, dispatch_get_main_queue(), ^{
[self.yourTextField becomeFirstResponder];
});
對於 Swift 3:
DispatchQueue.main.async(execute: {() -> Void in
let strongSelf: WalkthroughViewController = self
strongSelf. textField.becomeFirstResponder()
})
我不必指定 ViewController 的索引位置
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.