设置UINavigationBar外观whenContainedInInstancesOf到我的viewcontroller不工作

时间:2017-02-22 20:48:15

标签: ios swift uinavigationbar appearance bartintcolor

我想在某个viewcontroller中设置白色barTintColor 我设置了$table = Tablename::get(); foreach ($table as $value){ echo $value->22 // Getting column 22 from table } 对于所有默认颜色,但当我使用外观whenContainedInInstancesOf  它没有改变我的viewcontroller

UINavigationBar.appearance().barTintColor

有什么想法吗?我在我的viewcontroller中试过这个

UINavigationBar.appearance(whenContainedInInstancesOf: [MyViewController.self]).barTintColor = .white

但是当屏幕消失时我必须将颜色设置回默认值。我不想这样做。我该怎么办?

4 个答案:

答案 0 :(得分:4)

UINavigationBar包含在UINavigationController而非UIViewController

表示您需要创建自定义UINavigationController

空班级将完成这项工作

class NavigationController: UINavigationController {}

然后我们可以使用它

UINavigationBar.appearance(whenContainedInInstancesOf: [NavigationController.self]).barTintColor = .white

可以找到示例here

答案 1 :(得分:4)

@zombie有正确的答案。我刚刚用UISearchBar遇到过这个问题。这是答案的Swift 3版本。在此示例中,使用.red作为示例UIColor

 let textField = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
 textField.tintColor = .red

 let searchBar = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
 searchBar.setTitleTextAttributes([NSForegroundColorAttributeName: .red], for: .normal)
 searchBar.setTitleTextAttributes([NSForegroundColorAttributeName: .red], for: .disabled)

Sample Image

答案 2 :(得分:1)

斯威夫特4:

    // For the cursor color
    let textField = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
    textField.tintColor = .lightBlue

    // For the cancel button on search bar
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.white], for: .normal)
    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.lightText], for: .disabled)

答案 3 :(得分:0)

UIBarButtonItem.appearance(whenContainedInInstancesOf: [UIImagePickerController.self]).setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.black], for: .normal)

这对我有用!