代码之家  ›  专栏  ›  技术社区  ›  JsW

如何在Swift中复制实例

  •  0
  • JsW  · 技术社区  · 7 年前

    例如。我有一个 UISearchBar

    我认为可以通过实施 copy(with:) ( copyWithZone: )的 NSObject 然后打电话给 copy() .

    但我不知道的是 copyWithZone 在斯威夫特。

    我只需要一份 UISearchController 搜索栏 .

    如何做到这一点?

    1 回复  |  直到 7 年前
        1
  •  1
  •   AamirR    7 年前

    搜索栏 不能

    您必须创建 UISearchBar 创建保存设置的结构,如下所示:

    struct SearchBarConfig {
        var placeholder: String?
        var isTranslucent: Bool
    
        // Display Attributes
        var barStyle: UIBarStyle
        var barTintColor: UIColor?
        var searchBarStyle: UISearchBarStyle
        var tintColor: UIColor!
    
        init(isTranslucent: Bool, barStyle: UIBarStyle, searchBarStyle: UISearchBarStyle) {
            self.isTranslucent = isTranslucent
            self.barStyle = barStyle
            self.searchBarStyle = searchBarStyle
        }
    }
    
    let existingSearchBar = UISearchBar()
    var existingStatusBarConfig = SearchBarConfig(
        isTranslucent: existingSearchBar.isTranslucent,
        barStyle: existingSearchBar.barStyle,
        searchBarStyle: existingSearchBar.searchBarStyle)