【问题标题】:Change UIBarButtonItem from UISearchBar从 UISearchBar 更改 UIBarButtonItem
【发布时间】:2015-01-08 10:37:30
【问题描述】:

我想更改 iOS 8 中 UISearchBar 内取消按钮的文本字体和颜色。我已经尝试了 iOS 6 和 7 的解决方案,但它们似乎不起作用。

【问题讨论】:

  • 你能告诉我们你的尝试吗?

标签: swift ios8 uisearchbar uibarbuttonitem uicolor


【解决方案1】:

这不是我想要的解决方案,但它确实有效。

let cancelButtonAttributes: NSDictionary = [NSFontAttributeName: FONT_REGULAR_16!, NSForegroundColorAttributeName: COLOR_BLUE]
UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes, forState: UIControlState.Normal)

【讨论】:

  • 这也对我有用。 swift 2 的小修改,删除了字体属性和常量。 - 让 cancelButtonAttributes: NSDictionary = [NSForegroundColorAttributeName: UIColor.blackColor()] UIBarButtonItem.appearance().setTitleTextAttributes(cancelButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal)
  • 这会改变所有栏按钮项的颜色,而不仅仅是 UISearchBar 的取消按钮。
【解决方案2】:

你要找的是:

//The tintColor below will change the colour of the cancel button
searchBar.tintColor = UIColor.blueColor()

但是当我们讨论这个主题时,这些也很有用:

searchBar.placeholder = "Placeholder"
//The barTintColor changes the colour of the flashing text indicator
searchBar.barTintColor = UIColor.redColor()
//This sets the cancel button to show up
searchBar.setShowsCancelButton(true, animated: true)
//This makes the searchBar become active 
searchBar.becomeFirstResponder()

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    相关资源
    最近更新 更多