문제

SearchBar의 색조 색이 흰색으로 표시되기를 원합니다 (취소 버튼이 흰색이어야합니다).틴트 색이 흰색 일 때 커서가 보이지 않습니다.커서 색을 별도로 설정하는 방법이 있습니까?

도움이 되었습니까?

해결책

취소 버튼을 원하는 색상으로 색조를 설정 한 다음 uiAppArtance Protocol 텍스트 필드의 색조 색을 변경하여 커서를 원하는 색상으로 변경하십시오.예 :

[self.searchBar setTintColor:[UIColor whiteColor]];                
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTintColor:[UIColor darkGrayColor]];
.

다른 팁

기능을 좋아한다면, 스위프트의 한 라이널을 짜증나면 벤자민의 루프가 이렇게합니다 :

searchController.searchBar.tintColor = UIColor.whiteColor()

searchController.searchBar.subviews[0].subviews.flatMap(){ $0 as? UITextField }.first?.tintColor = UIColor.blueColor()
.

스위프트 3.0 및 4 버전

searchController.searchBar.tintColor = .white        
UITextField.appearance(whenContainedInInstancesOf: [type(of: searchController.searchBar)]).tintColor = .black
.

SearchBar는 선택 사항 일 수 없습니다.

for-where 구문 (루프 없음 필요 없음)을 사용하는 컴팩트 스위프트 2.0 솔루션 :

// Make SearchBar's tint color white to get white cancel button.
searchBar.tintColor = UIColor.white()

// Loop into it's subviews and find TextField, change tint color to something else.
for subView in searchBar.subviews[0].subviews where subView.isKindOfClass(UITextField) {
        subView.tintColor = UIColor.darkTextColor()
}
.
searchBar.tintColor = [UIColor whiteColor];
searchBar.backgroundColor = [UIColor clearColor];
for ( UIView *v in [searchBar.subviews.firstObject subviews] )
{
    if ( YES == [v isKindOfClass:[UITextField class]] )
    {
        [((UITextField*)v) setTintColor:[UIColor blueColor]];
        break;
    }
}
.

여기에 이미지 설명

이것은 Swift에서 나를 위해 일하는 것처럼 보였습니다.

    searchController.searchBar.tintColor = UIColor.whiteColor()
    UITextField.appearanceWhenContainedInInstancesOfClasses([searchController.searchBar.dynamicType]).tintColor = UIColor.blackColor()
.

Swift에서 동일한 작업을 원합니다. 여기에 많은 문제가 발생한 후에 Accros와 왔습니다.

override func viewWillAppear(animated: Bool) {
    self.searchBar.tintColor = UIColor.whiteColor()

    let view: UIView = self.searchBar.subviews[0] as! UIView
    let subViewsArray = view.subviews

    for (subView: UIView) in subViewsArray as! [UIView] {
        println(subView)
        if subView.isKindOfClass(UITextField){
            subView.tintColor = UIColor.blueColor()
        }
    }

}
.

다음 코드가있는 UISearchBar에 확장자를 추가 할 것입니다.

extension UISearchBar {
    var cursorColor: UIColor! {
        set {
            for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
                subView.tintColor = newValue
            }
        }
        get {
            for subView in self.subviews[0].subviews where ((subView as? UITextField) != nil) {
                return subView.tintColor
            }
            // Return default tintColor
            return UIColor.eightBit(red: 1, green: 122, blue: 255, alpha: 100)
        }
    }
}
.

iOS 9 이상

취소 버튼과 TextField에 대해 TintColor를 설정하는 가장 쉬운 방법은 다음을 사용합니다.

[self.searchBar setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:UIColor.blueColor];
.

이것은 가장 쉬운 해결책입니다.

let textField = self.searchBar.value(forKey: "searchField") as! UITextField

    textField.tintColor = UIColor.white
.

swift 5에서 가장 쉽습니다 5 :

    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).tintColor = .black
.

여기에 Felipe의 답변 (SWIFT 4.2)

의 "기능적"버전이 있습니다.
// Make SearchBar's tint color white to get white cancel button.
searchBar.tintColor = .white

// Get the TextField subviews, change tint color to something else.
if let textFields = searchBar.subviews.first?.subviews.compactMap({ $0 as? UITextField }) {
        textFields.forEach { $0.tintColor = UIColor.darkGray }
}
.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top