在UISearchBar中删除搜索图标的动画

时间:2017-05-28 12:05:54

标签: ios iphone swift swift3

无论如何都要删除UISearchBar搜索图标的动画,该图标在响应时会从UISearchBar的中心向左侧动画?

搜索图标应显示在左侧而不是UISearchBar的中心,并且在用户响应时不应设置动画。

1 个答案:

答案 0 :(得分:0)

import Foundation
import UIKit

@objc public class LeftAlignedSearchBar: UISearchBar, UISearchBarDelegate {
    override public var placeholder:String? {
        didSet {
            if #available(iOS 9.0, *) {
                if let text = placeholder {
                    if text.characters.last! != " " {
                        let attr = UITextField.appearanceWhenContainedInInstancesOfClasses([LeftAlignedSearchBar.self]).defaultTextAttributes
                        let maxSize = CGSizeMake(self.bounds.size.width - 86, 40)
                        let widthText = text.boundingRectWithSize( maxSize, options: .UsesLineFragmentOrigin, attributes:attr, context:nil).size.width
                        let widthSpace = " ".boundingRectWithSize( maxSize, options: .UsesLineFragmentOrigin, attributes:attr, context:nil).size.width
                        let spaces = floor((maxSize.width - widthText) / widthSpace)
                        let newText = text + ((Array(count: Int(spaces), repeatedValue: " ").joinWithSeparator("")))
                        if newText != text {
                            placeholder = newText
                        }
                    }
                }
            }
        }
    }
}

来源:https://medium.com/@maximbilan/ios-left-aligned-uisearchbar-b51ef36b6e1b