当键盘出现时移动文本框(Swift)

233

我正在使用Swift编写iOS程序,我正在使用这段代码来移动UITextField,但它不起作用。我正确调用了函数keyboardWillShow,但文本框没有移动。我正在使用自动布局。

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")
    }
}

2
使用项目文件的步骤指南:https://www.codebeaulieu.com/43/Triggering-NSNotificationCenter-notifications-from-the-UI - Dan Beaulieu
可能deinit和viewDidLoad不平衡。 - Ricardo
根据苹果的文档和个人经验。 这是我使用UIScrollView移动TF的git仓库: https://github.com/29satnam/MoveTextFieldWhenKeyboardAppearsSwift - Codetard
38个回答

340

现有答案需要进行一些改进。

首先,UIKeyboardWillChangeFrameNotification 可能是最好的通知,因为它处理的不仅仅是键盘的显示/隐藏,还包括由于键盘更改(语言、使用第三方键盘等)和旋转而引起的更改(但请注意下面的评论指出,应该处理键盘将隐藏以支持硬件键盘连接)。

其次,可以从通知中提取动画参数,以确保动画正确运行。

如果您熟悉强制解包字典代码,可能还有更多的清理选项可供选择。

 class MyViewController: UIViewController {

   // This constraint ties an element at zero points from the bottom layout guide
   @IBOutlet var keyboardHeightLayoutConstraint: NSLayoutConstraint?
 
   override func viewDidLoad() {
     super.viewDidLoad()
     NotificationCenter.default.addObserver(self,
       selector: #selector(self.keyboardNotification(notification:)),
       name: UIResponder.keyboardWillChangeFrameNotification,
       object: nil)
   }
 
   deinit {
     NotificationCenter.default.removeObserver(self)
   }
 
   @objc func keyboardNotification(notification: NSNotification) {
     guard let userInfo = notification.userInfo else { return }

     let endFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue
     let endFrameY = endFrame?.origin.y ?? 0
     let duration:TimeInterval = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
     let animationCurveRawNSN = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
     let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
     let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)

     if endFrameY >= UIScreen.main.bounds.size.height {
       self.keyboardHeightLayoutConstraint?.constant = 0.0
     } else {
       self.keyboardHeightLayoutConstraint?.constant = endFrame?.size.height ?? 0.0
     }

     UIView.animate(
       withDuration: duration,
       delay: TimeInterval(0),
       options: animationCurve,
       animations: { self.view.layoutIfNeeded() },
       completion: nil)
   }
}

1
@JosephLord 不错。但是我发现当键盘隐藏时,这个方法不起作用,因为 endFrame?.size.height 不为空。我在 iOS 8.3 iPad 模拟器上运行,竖屏模式下得到的结束框架是 UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 1024}, {768, 264}}";。使用的是 Xcode6.3 beta4。 - Hlung
8
如果键盘没有隐藏,尝试使用以下代码:如果 endFrame?.origin.y >= UIScreen.mainScreen().bounds.size.height { self.keyboardHeightLayoutConstraint?.constant = 0.0 } else { self.keyboardHeightLayoutConstraint?.constant = endFrame.size.height } 该代码可使键盘高度归零或设置为endFrame的高度。 - Gabriel Goncalves
4
keyBoardHeightLayoutConstraint是在InterfaceBuilder中定义的一个约束,用于将你希望移动/收缩的视图底部限制为底部布局指南或视图控制器的主视图底部。该常量最初设置为零,并且会根据键盘的出现或大小变化而进行调整,以腾出空间。 - Joseph Lord
2
请注意,即使iOS键盘消失,当硬件键盘连接时,.UIKeyboardWillChangeFrame不会触发。您需要同时观察.UIKeyboardWillHide以捕获该边缘情况。 - jamesk
1
代码对我没用,所以我只是删除了if/else,并用self.<enterbuttonname>.frame.origin.y = endFrameY - self.<enterbuttonname>.frame.height进行了替换。 - JavaBeast
显示剩余27条评论

133

如果您正在使用自动布局,我假设您已设置了底部超级视图约束。 如果是这种情况,您只需更新约束的值即可。以下是如何使用一点动画实现它。

func keyboardWasShown(notification: NSNotification) {
    let info = notification.userInfo!
    let keyboardFrame: CGRect = (info[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()

    UIView.animateWithDuration(0.1, animations: { () -> Void in
        self.bottomConstraint.constant = keyboardFrame.size.height + 20
    })
}

硬编码的“20”只是为了将文本字段弹出到键盘上方一点,否则键盘的顶部边距和文本字段的底部边距会接触。

当键盘消失时,将约束的值重置为其原始值。


1
请问您能解释一下如何定义它吗?谢谢!我总是在Storyboard上控制所有内容。 - Pedro Manfredi
4
bottomConstraint 是我给这个约束命名的。我选择了这个约束,拖动并创建了一个 IBOutlet 并给了它这个名字。你可以像对待其他 UI 元素(如按钮和文本框)一样,创建约束的 IBOutlet。 - Isuru
3
这个答案很好用,但是对我来说动画立即发生了。请查看如何动画地更改约束条件?以了解如何正确地进行动画。 - Adam Johns
2
@vinbhai4u,你需要注册UIKeyboardWillShowNotification通知。请查看原帖中的代码。 - Isuru
8
为了让约束变化有动画效果,需要在 animateWithDuration 之外更新常量,并在动画块内调用 self.view.layoutIfNeeded() - Max
显示剩余14条评论

128

一个简单的解决方案是利用键盘高度的常量将视图向上移动。

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);
}

@objc func keyboardWillShow(sender: NSNotification) {
     self.view.frame.origin.y = -150 // Move view 150 points upward 
}

@objc func keyboardWillHide(sender: NSNotification) {
     self.view.frame.origin.y = 0 // Move view to original position  
}

Swift 5:

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(sender:)), name: UIResponder.keyboardWillShowNotification, object: nil);

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(sender:)), name: UIResponder.keyboardWillHideNotification, object: nil);

4
我喜欢这个简单的解决方案。但是我添加了一个keyboardShowing布尔值,因为我有多个textfield。只有在键盘显示时才移动它一次。谢谢。 - Ken Roy
2
不要移动视图,移动textView。 - ericgu
1
如果用户切换输入语言,则该代码将保留视图的y值。 - Jeffrey Neo
@Sashi,你能发一下你的解决方案吗?我目前也遇到了同样的问题。 - kye
7
дҪҝз”Ёself.view.frame.origin.y -= 150зҡ„жӣҝд»Јж–№жі•жҳҜдҪҝз”Ёself.view.frame.origin.y = -150пјҢиҖҢдҪҝз”Ёself.view.frame.origin.y += 150зҡ„жӣҝд»Јж–№жі•жҳҜдҪҝз”Ёself.view.frame.origin.y = 0гҖӮиҝҷеҸҜд»ҘйҳІжӯўи§ҶеӣҫеңЁжҜҸж¬Ўи§Ұж‘ёж–°еӯ—ж®ө时移еҠЁ150гҖӮ - gunwin
显示剩余9条评论

47

要在编辑文本字段时移动视图,请尝试以下方法,我已经应用了这个方法,

选项1:- **更新Swift 5.0和iPhone X、XR、XS和XS Max 使用NotificationCenter进行移动。

  • func viewWillAppear(_ animated: Bool)中注册此通知

  • func viewWillDisappear(_ animated: Bool)中注销此通知

注意:- 如果您不注销它,它将从子类调用并导致崩溃或其他问题。

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillShow(notification:)), name:  UIResponder.keyboardWillShowNotification, object: nil )
}
override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
}

@objc func keyboardWillShow( notification: Notification) {
    if let keyboardFrame: NSValue = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
        var newHeight: CGFloat
        let duration:TimeInterval = (notification.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
        let animationCurveRawNSN = notification.userInfo![UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber
        let animationCurveRaw = animationCurveRawNSN?.uintValue ?? UIView.AnimationOptions.curveEaseInOut.rawValue
        let animationCurve:UIView.AnimationOptions = UIView.AnimationOptions(rawValue: animationCurveRaw)
        if #available(iOS 11.0, *) {
            newHeight = keyboardFrame.cgRectValue.height - self.view.safeAreaInsets.bottom
        } else {
            newHeight = keyboardFrame.cgRectValue.height
        }
        let keyboardHeight = newHeight  + 10 // **10 is bottom margin of View**  and **this newHeight will be keyboard height**
        UIView.animate(withDuration: duration,
                       delay: TimeInterval(0),
                       options: animationCurve,
                       animations: {
                        self.view.textViewBottomConstraint.constant = keyboardHeight **//Here you can manage your view constraints for animated show**
                        self.view.layoutIfNeeded() },
                       completion: nil)
    }
}

选项 2 :它能正常工作。

func textFieldDidBeginEditing(textField: UITextField) {
        self.animateViewMoving(up: true, moveValue: 100)
}
func textFieldDidEndEditing(textField: UITextField) {
        self.animateViewMoving(up: false, moveValue: 100)
}

func animateViewMoving (up:Bool, moveValue :CGFloat){
    var movementDuration:NSTimeInterval = 0.3
    var movement:CGFloat = ( up ? -moveValue : moveValue)
    UIView.beginAnimations( "animateView", context: nil)
    UIView.setAnimationBeginsFromCurrentState(true)
    UIView.setAnimationDuration(movementDuration )
    self.view.frame = CGRectOffset(self.view.frame, 0,  movement)
    UIView.commitAnimations()
}

我从这个来源得到了答案:Swift中的UITextField在键盘出现时向上移动

在Swift 4中 ---

func textFieldDidBeginEditing(_ textField: UITextField) {
        animateViewMoving(up: true, moveValue: 100)
    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        animateViewMoving(up: false, moveValue: 100)
    }
    func animateViewMoving (up:Bool, moveValue :CGFloat){
        let movementDuration:TimeInterval = 0.3
        let movement:CGFloat = ( up ? -moveValue : moveValue)
        UIView.beginAnimations( "animateView", context: nil)
        UIView.setAnimationBeginsFromCurrentState(true)
        UIView.setAnimationDuration(movementDuration ) 
        self.view.frame = self.view.frame.offsetBy(dx: 0, dy: movement)
        UIView.commitAnimations()
    }

1
@Jogendra.Com,感谢您的辛勤工作。但是,它在iPhone 5、4s和6上效果最佳。但是,我该如何在iPhone 6plus和iPad(更高版本)上禁用它? - Thiha Aung
如果您使用选项1,请确保添加一个IBOutlet约束。您将创建一个要使用自动布局调整大小的约束,然后将其拖放到视图控制器中以创建一个IBOutlet,我将其引用为self.iboutletConstraint.constant在animate函数中。此外,这不会在隐藏键盘时重新调整输出,我通过将约束重置为其原始值来处理它。 - Hammad Tariq

22

我喜欢简洁的Swift代码。所以这里是我能想到的最简洁的代码,用于在键盘出现时将文本视图向上/向下移动。它目前在一个iOS8 / 9 Swift 2生产应用中运行。

更新(2016年3月): 我尽可能地简化了以前的代码。此外,这里有很多受欢迎的答案硬编码键盘高度和动画参数。没有必要这样做,更不用说这些答案中的数字并不总是与我在我的6s + iOS9上看到的实际值相符(键盘高度为226,持续时间为0.25,动画曲线为7)。无论如何,从系统直接获取这些值几乎不需要额外的代码。请参见下面。

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "animateWithKeyboard:", name: UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "animateWithKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
}

func animateWithKeyboard(notification: NSNotification) {

    // Based on both Apple's docs and personal experience, 
    // I assume userInfo and its documented keys are available.
    // If you'd like, you can remove the forced unwrapping and add your own default values.

    let userInfo = notification.userInfo!
    let keyboardHeight = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue().height
    let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
    let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt
    let moveUp = (notification.name == UIKeyboardWillShowNotification)

    // baseContraint is your Auto Layout constraint that pins the
    // text view to the bottom of the superview.

    baseConstraint.constant = moveUp ? -keyboardHeight : 0

    let options = UIViewAnimationOptions(rawValue: curve << 16)
    UIView.animateWithDuration(duration, delay: 0, options: options,
        animations: {
            self.view.layoutIfNeeded()
        },
        completion: nil
    )

}

注意:此代码覆盖了最常见/通用的情况。然而,可能需要更多代码来处理不同的方向和/或自定义键盘 这里有一篇深入的文章,介绍如何使用iOS键盘。如果您需要处理每种情况,这可能会有所帮助。


似乎是Swift 1.1版本的代码,我认为在Swift 1.2版本中无法编译,因为它使用as进行强制转换。as!可能会起作用,但正如您在此页面的其他地方所看到的,我避免使用强制转换和强制解包。 - Joseph Lord
现在可以在Swift 1.2中编译。我还在代码中添加了一个关于强制解包的注释。干杯。 - scootermg
糟糕,我的意思是Swift 2。 - scootermg
根据你是如何连接你的 baseConstraint,它可能是 baseConstraint.constant = moveUp ? keyboardHeight : 0 而不是 baseConstraint.constant = moveUp ? -keyboardHeight : 0 - limfinity

15

编辑: 我建议一个更简单、更清晰的解决方案。只需将底部间距约束的类更改为KeyboardLayoutConstraint。它会自动扩展到键盘高度。

    // You have to set this up in storyboard first!. 
    // It's a vertical spacing constraint between view and bottom of superview.
    @IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint! 

    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillShowNotification, object: nil);
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillHideNotification, object: nil);
    }

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func keyboardNotification(notification: NSNotification) {

        let isShowing = notification.name == UIKeyboardWillShowNotification

        if let userInfo = notification.userInfo {
            let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
            let endFrameHeight = endFrame?.size.height ?? 0.0
            let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
            let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
            let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
            let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
            self.bottomSpacingConstraint?.constant = isShowing ? endFrameHeight : 0.0
            UIView.animateWithDuration(duration,
                delay: NSTimeInterval(0),
                options: animationCurve,
                animations: { self.view.layoutIfNeeded() },
                completion: nil)
        }
    }

请问您能解释一下您的修改吗?我无法让它正常工作。我有一个UIScrollView,底部有一个按钮。我将类设置在底部的边距约束上。 - schw4ndi
@schw4ndi,你的底部约束绑定到哪些视图上了?它应该将滚动视图的底部连接到该滚动视图的父视图的底部。 - Hlung
哦,谢谢,我有按钮和滚动视图之间的约束。 - schw4ndi

10

我正在使用 Swift 4 进行工作,而且我已经解决了这个问题,而不需要使用任何额外的底部约束。请看我的代码,它确实在我的情况下起作用。

1)在did load中添加通知观察器

override func viewDidLoad() {
        super.viewDidLoad()
        setupManager()
        // Do any additional setup after loading the view.
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
    }

2)移除通知观察者,例如:

deinit {
        NotificationCenter.default.removeObserver(self)
    }

3) 添加键盘显示/隐藏的方法,例如

 @objc func keyboardWillShow(notification: NSNotification) {
            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                UIView.animate(withDuration: 0.1, animations: { () -> Void in
                    self.view.frame.origin.y -= keyboardSize.height
                    self.view.layoutIfNeeded()
                })
            }
        }

@objc func keyboardWillHide(notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
            UIView.animate(withDuration: 0.1, animations: { () -> Void in
                self.view.frame.origin.y += keyboardSize.height
                self.view.layoutIfNeeded()
            })
        }
    }

4)添加文本字段代理并添加touchesBegan方法。当触摸屏幕上的文本字段以外区域时,可用于隐藏键盘。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)

    }

需要是 UIKeyboardFrameEndUserInfoKey - Micro
1
NSNotification.Name.UIKeyboardWillShow is Renamed to UIResponder.keyboardWillShowNotification also UIKeyboardFrameBeginUserInfoKey to UIResponder.keyboardFrameBeginUserInfoKey - smj

9
你可以使用这个库,并在`appDidFinishedLaunching`函数中只添加一行代码就完成了。
func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    IQKeyboardManager.sharedManager().enable = true
    return true
}

IQKeyboardManager - 调整视图以适应键盘出现
链接 - https://github.com/hackiftekhar/IQKeyboardManager


是的,这很好。但是你真的需要为这个简单的问题使用这个库吗?这只是方便而已。 - Rhenz
@Rhenz,仅为方便起见,我们可以选择不这样做,但是IQKeyboardManager除了将UI移动到键盘上方之外还有很多其他功能。 - Abdul Karim

8

这是@JosephLord和@Hlung答案的改进版。它适用于无论你是否有标签栏。它可以完美地将被键盘移动的视图恢复到原始位置。

// You have to set this up in storyboard first!. 
// It's a vertical spacing constraint between view and bottom of superview.
@IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint! 

override func viewDidLoad() {
        super.viewDidLoad()            

        //    Receive(Get) Notification
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillHideNotification, object: nil)


        self.originalConstraint = self.keyboardHeightLayoutConstraint?.constant //for original coordinate.
}

func keyboardNotification(notification: NSNotification) {
        let isShowing = notification.name == UIKeyboardWillShowNotification

        var tabbarHeight: CGFloat = 0
        if self.tabBarController? != nil {
            tabbarHeight = self.tabBarController!.tabBar.frame.height
        }
        if let userInfo = notification.userInfo {
            let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
            let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
            let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
            let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
            let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
            self.keyboardHeightLayoutConstraint?.constant = isShowing ? (endFrame!.size.height - tabbarHeight) : self.originalConstraint!
            UIView.animateWithDuration(duration,
                delay: NSTimeInterval(0),
                options: animationCurve,
                animations: { self.view.layoutIfNeeded() },
                completion: nil)
        }
}

8

完整的键盘管理代码。

        override func viewWillAppear(_ animated: Bool) {
            NotificationCenter.default.addObserver(self, selector: #selector(StoryMediaVC.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
            NotificationCenter.default.addObserver(self, selector: #selector(StoryMediaVC.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
        }
        override func viewWillDisappear(_ animated: Bool) {
            NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil)
            NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil)
        }
        @objc func keyboardWillShow(notification: NSNotification) {
            guard let userInfo = notification.userInfo else {return}
            guard let keyboardSize = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue else {return}
            let keyboardFrame = keyboardSize.cgRectValue

            if self.view.bounds.origin.y == 0{
                self.view.bounds.origin.y += keyboardFrame.height
            }
        }


        @objc func keyboardWillHide(notification: NSNotification) {
            if self.view.bounds.origin.y != 0 {
                self.view.bounds.origin.y = 0
            }
        }

运行得非常顺畅。 - Fahim Rahman
如果你没有任何解决方案,那么使用这个完美的解决方案来拯救你。 - Anurag Soni
完美运行 - Gleny Rebellow

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接