9

搜索时是否可以在 UISearchBar 中显示 UIActivityIndi​​cator?

4

3 回答 3

17

iOS 13更新。

ios13

您可以在 Swift 4+ 中使用以下扩展。用法:

显示活动: searchBar.isLoading = true

隐藏活动: searchBar.isLoading = false

UISearchBar 扩展

extension UISearchBar {

    public var textField: UITextField? {
        if #available(iOS 13.0, *) {
            return self.searchTextField
        } else {
            let subViews = subviews.flatMap { $0.subviews }
            guard let textField = (subViews.filter { $0 is UITextField }).first as? UITextField else {
                return nil
            }
            return textField
        }
    }

    public var activityIndicator: UIActivityIndicatorView? {
        return textField?.leftView?.subviews.flatMap{ $0 as? UIActivityIndicatorView }.first
    }

    var isLoading: Bool {
        get {
            return activityIndicator != nil
        } set {
            if newValue {
                if activityIndicator == nil {
                    let newActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
                    newActivityIndicator.transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
                    newActivityIndicator.startAnimating()
                    newActivityIndicator.backgroundColor = UIColor.white
                    textField?.leftView?.addSubview(newActivityIndicator)
                    let leftViewSize = textField?.leftView?.frame.size ?? CGSize.zero
                    newActivityIndicator.center = CGPoint(x: leftViewSize.width/2, y: leftViewSize.height/2)
                }
            } else {
                activityIndicator?.removeFromSuperview()
            }
        }
    }
}
于 2017-08-27T13:14:56.660 回答
10

是的你可以。

开始搜索时,创建一个活动指示器并将其添加为UISearchBarusingaddSubView方法的子视图。完成搜索后,使用removeFromSuperView方法将其从搜索栏中删除。

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//set frame for activity indicator
[searchBar addSubview: spinner];
[spinner startAnimating];

请参阅此链接以获取替代方法

于 2012-11-07T18:19:22.507 回答
4

发现没有一个广告解决方案适用于 iOS13...这是我自己的,它是搜索栏的子类,应该更强大以备将来使用

class SearchBar: UISearchBar {

    var activityIndicator: UIActivityIndicatorView?
    
    var isLoading: Bool {
        get {
            return activityIndicator != nil
        } set {
            if newValue {
                if activityIndicator == nil {
                    
                    guard let leftView = searchTextField.leftView else {
                        return
                    }
                    
                    let ai = UIActivityIndicatorView(style: .medium)
                    ai.frame = self.convert(leftView.frame, from: leftView.superview)
                    self.addSubview(ai)
                    
                    ai.startAnimating()
                    leftView.isHidden = true
                    activityIndicator = ai
                    
                }
            } else {
                activityIndicator?.removeFromSuperview()
                activityIndicator = nil
                
                guard let leftView = searchTextField.leftView else {
                    return
                }
                leftView.isHidden = false
            }
        }
    }
    
}
于 2020-07-28T09:12:16.707 回答