Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何以编程方式更改UISearchBar或UITableViewHeader的UIFont?_Ios_Swift_Uitableview_Uiview_Uisearchbar - Fatal编程技术网

Ios 如何以编程方式更改UISearchBar或UITableViewHeader的UIFont?

Ios 如何以编程方式更改UISearchBar或UITableViewHeader的UIFont?,ios,swift,uitableview,uiview,uisearchbar,Ios,Swift,Uitableview,Uiview,Uisearchbar,我有一个UITableView,我通过编程将UISearchController作为标题添加到该视图中。因为我是以编程方式添加它的,所以无法从情节提要中更改字体。我需要更改UISearchBar字体以匹配应用程序其余部分的字体。我需要更改UISearchBar或整个tableViewHeader 我看到objective-c中有一个类似的问题,但我正在Swift中开发我的应用程序,我试图将另一个问题的答案转换为以下代码,但仍然无法工作: let lightFont = UIFont(name:

我有一个
UITableView
,我通过编程将
UISearchController
作为标题添加到该视图中。因为我是以编程方式添加它的,所以无法从情节提要中更改字体。我需要更改
UISearchBar
字体以匹配应用程序其余部分的字体。我需要更改
UISearchBar
或整个
tableViewHeader

我看到objective-c中有一个类似的问题,但我正在Swift中开发我的应用程序,我试图将另一个问题的答案转换为以下代码,但仍然无法工作:

let lightFont = UIFont(name: "GothamRounded-Light", size: 15)

        for subview in searchController.searchBar.subviews {
            if subview is UITextField{

                (subview as! UITextField).font = lightFont

            }

        }

您需要在其子视图中循环查找UITextField(属性无法访问)

然后您可以设置它的字体。

if #available(iOS 9.0, *) {
    UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.blueColor()
    UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).font = UIFont.systemFontOfSize(17)
}
您需要在其子视图中循环查找UITextField(属性无法访问)

然后您可以设置它的字体。

试试这个:

if #available(iOS 9.0, *) {
    UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.blueColor()
    UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).font = UIFont.systemFontOfSize(17)
}
let textFieldInSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInSearchBar?.font = UIFont.systemFont(ofSize: 10)
试试这个:

let textFieldInSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInSearchBar?.font = UIFont.systemFont(ofSize: 10)

在viewDidLoad()中添加以下代码


这将为
ui搜索栏中的占位符和文本设置自定义字体

viewDidLoad()中添加以下代码


这将为
ui搜索栏中的占位符和文本设置自定义字体

谢谢,看起来不错,但当我运行它时,字体没有改变。我用我正在使用的代码更新了我的问题!我认为问题在于它找不到属于UITextField的子视图。你知道为什么会发生这种情况吗?谢谢,看起来不错,但当我运行它时,字体没有改变。我用我正在使用的代码更新了我的问题!我认为问题在于它找不到属于UITextField的子视图。你知道为什么会发生这种情况吗?你能解释一下这里发生了什么,并说明为什么你觉得这是一个好答案吗?你能解释一下这里发生了什么,并说明为什么你觉得这是一个好答案吗?