Swift modifies the text, color of the button in the Search bar (Uisearchbar)

Source: Internet
Author: User
Tags uikit

When we use the Search bar (uisearchbar), if you set it to "shows Cancel button", there is a Cancel button on the right side of the input box (the title text is cancel).


Sometimes we want to change this button to something else, like "search." Although Searchbar does not provide a way or a property to directly modify the text of this button, we can implement it in a different way.

1, modify the text of the Searchbar Cancel button

First get the cancellation button in the Searchbar, and then set the button title can be.


Import Uikit

Class Viewcontroller:uiviewcontroller {

@IBOutlet weak var searchbar:uisearchbar!

Override Func Viewdidload () {
Super.viewdidload ()

Let UIButton = Searchbar.valueforkey ("CancelButton") as! UIButton
Uibutton.settitle ("Search", ForState:UIControlState.Normal)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

2, modify the text color of the Searchbar Cancel button

Also get the Cancel button in the Searchbar first, then set the button Titlecolor.


Import Uikit

Class Viewcontroller:uiviewcontroller {

@IBOutlet weak var searchbar:uisearchbar!

Override Func Viewdidload () {
Super.viewdidload ()

Let UIButton = Searchbar.valueforkey ("CancelButton") as! UIButton
Uibutton.settitle ("Search", ForState:UIControlState.Normal)
Uibutton.settitlecolor (Uicolor.orangecolor (), forstate:. Normal)
}

Override Func didreceivememorywarning () {
Super.didreceivememorywarning ()
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.