swift - UISearchBar 显示取消按钮动画完成

标签 swift animation uisearchbar

我需要知道 UISearchBar 的取消按钮在调用后何时完成他的动画

searchBar.setShowsCancelButton(false, animated: true)

我试过了

UIView.animateWithDuration(3, animations: {
    self.searchBarView.searchBar.showsCancelButton = false
         }, completion: {finished in
         print(finished)
})

但是会立即触发完成 block ,任何解决方案都将不胜感激

最佳答案

您可以使用 layoutIfNeeded() 来更新 UIView.animateWithDuration(...) 中的 View

试试这段代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet var searchBar: UISearchBar!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func touchUpInside(sender: UIButton) {
    searchBar.showsCancelButton = !searchBar.showsCancelButton
    UIView.animateWithDuration(3, animations: {
        self.searchBar.layoutIfNeeded()
        }, completion: {finished in
            print("Animation finished")
    })
}
}

和 Storyboard: enter image description here

关于swift - UISearchBar 显示取消按钮动画完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38961883/

相关文章:

xcode - 将 Swift 与 OS X 首选项面板插件一起使用

ios - 如何隐藏/禁用 UISearchBar 范围栏

iphone - 首次使用前 UISearchBar 大小不一致?

ios - 我将如何在 iOS 的谷歌地图中添加搜索栏

ios - 我们可以在 Swift 中使结构中的 Codable 键不区分大小写吗

ios - 如何在扩展中设置只读属性?

javascript - 使用 Glorious JavaScript 库制作动画

javascript - jQuery UI ReplaceWith() 动画?

ios - iOS Swift 中的 core.async channel 用于解耦组件

javascript - 在网页上添加退出动画