Swift:如何在聚焦时使搜索栏具有动画效果?曾经工作?

时间:2017-09-22 21:14:16

标签: ios swift uitableview uisearchbar

好吧,我觉得好像有几个版本的Xcode以前这是默认的,我让它在过去的项目中工作,只是不知道为什么我不能让这个搜索栏动画发生在这个搜索栏现在Xcode 9。

我想要发生的是当搜索栏ISNT聚焦时,占位符文本居中:

enter image description here

然后当它点击并成为第一响应者时,文本保持对齐(两者之间的过渡似乎自动动画):

enter image description here

我发誓在没有任何自定义的情况下用于执行此操作的搜索栏。我怎样才能做到这一点?现在在我的tableview自定义标题中,我以编程方式设置我的搜索栏:

searchBar.placeholder = "Find an Episode."
        searchBar.backgroundColor = UIColor.clear
        searchBar.searchBarStyle = .minimal
        searchBar.layer.shadowOpacity = 0.0
        searchBar.barTintColor = thirdColorStr
        searchBar.tintColor = thirdColorStr
        searchBar.frame = CGRect(x: 0, y: 0, width: self.bounds.width, height: screenSize.height * (40/screenSize.height))
        self.addSubview(searchBar)
//        searchBar.center = CGPoint(x: self.bounds.width/2, y: (searchBar.bounds.height/2))
        searchBar.center = CGPoint(x: self.bounds.width/2, y: (searchBar.bounds.height)*0.8)
        searchBar.delegate = listVC

        for subView in searchBar.subviews {
            for subViewOne in subView.subviews {

                if let textField = subViewOne as? UITextField {

                    subViewOne.backgroundColor = secondColorStr
                    //print("HEREE:", textField.bounds.width/screenSize.width)

                    //use the code below if you want to change the color of placeholder
                    if let t = textField.value(forKey: "placeholderLabel") as? UILabel
                    {
                        t.textColor = thirdColorStr
                        t.font = iphoneFontThin
                        t.tintColor = thirdColorStr
                        t.backgroundColor = UIColor.clear

                    }
//                    if let clearButton = textField.value(forKey: "_clearButton") as? UIButton {
//                        // Create a template copy of the original button image
//                        let img = UIImage(named: "clear")
//                        clearButton.setImage(img, for: .normal)
//                        
//                        // Finally, set the image color
//                        //clearButton.tintColor = .red
//                    }
                    if let t = searchBar.value(forKey: "searchField") as? UITextField
                    {
                        t.textColor = barColorStr
                        t.font = iphoneFontThin
                        t.backgroundColor = secondColorStr

然后

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        self.searchBar.showsCancelButton = true
    }

    func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
        //searchBar.showsCancelButton = true
        resetCategory(newCat: overallCategory)
        searchBar.text = ""
        searchBar.resignFirstResponder() //kills keyboard
    }

我无法找到生成和动画的代码与上面的代码之间没有任何区别。

0 个答案:

没有答案