Ich muss die Farbe des Abbruchtaste-Texts von UISearchBar
in iOS7 ändern.
Normalerweise ist die Variable UISearchBar
Cancel textColor
blau und ich möchte textColor
in redColor
ändern.
Wie kann ich das ändern?
Ich habe Antworten auf meine eigenen Fragen gefunden.
Hier ist Code, fügen Sie in AppDelegate
ein, wenn Sie alle Abbrechen-Schaltflächen ändern möchten.
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor],
UITextAttributeTextColor,
[UIColor whiteColor],
UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)],
UITextAttributeTextShadowOffset,
nil]
forState:UIControlStateNormal];
Schnell:
let attributes = [NSForegroundColorAttributeName : UIColor.red]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
Wenn Sie nur die Textfarbe der Schaltfläche festlegen möchten, benötigen Sie nur eine Zeile:
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor redColor]];
Ein viel einfacher Weg ->
self.searchbar.tintColor = [UIColor darkGrayColor];
Sie können es so machen
[yourSearchBarName setTintColor:[UIColor whateverColorYouWant]];
Sie können die Unteransichten der UISearchBar
in - (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
wie folgt ändern.
UIView *view = [_searchBar.subviews objectAtIndex:0];
for (UIView *subView in view.subviews) {
if ([subView isKindOfClass:[UIButton class]]) {
UIButton *cancelButton = (UIButton *)subView;
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
}
}
Sie können die Schaltfläche zum Abbrechen der Suchleiste wie folgt formatieren
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTintColor:[UIColor whiteColor]];
[[UIBarButtonItem appearanceWhenContainedIn: [UISearchBar class], nil] setTitle:@"Your Text Here"];
Hoffe, dass es für dich funktioniert.
Arbeitete für Swift 4
appearance
-Funktion des UIAppearance
-Moduls verwenden -
Methode 1: - Zeige Abbrechen beim Laden mit searchBar
-
let attributes = [NSAttributedStringKey.foregroundColor : UIColor.red]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: .normal)
oder -
Methode 2: - Farbe der Abbrechen-Schaltfläche anzeigen, nachdem searchBar
angeklickt wurde -
UIBarButtonItem.appearance(whenContainedInInstancesOf:[UISearchBar.self]).tintColor = UIColor.red
Swift 3:
Verwenden Sie diesen Code, um die rote Farbe festzulegen (Text, Kurs, Schaltfläche).
searchController.searchBar.tintColor = .red
wenn Sie die Farbe der Abbrechen-Schaltfläche in Weiß ändern möchten, fügen Sie diesen Code hinzu
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .normal)
[[UISearchBar appearance] setTintColor:[UIColor redColor]];
Getestet auf Swift 4
let barButtonItem = UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self])
barButtonItem.title = NSLocalizedString("Cancel", comment: "")
barButtonItem.setTitleTextAttributes([.font : UIFont.systemFont(ofSize: 15.0, weight: .medium),
.foregroundColor : #colorLiteral(red: 0.1960784314, green: 0.1960784314, blue: 0.1960784314, alpha: 1)], for: .normal)
Mein Ansatz, den Cursor und die Schaltflächenfarbe unabhängig voneinander festzulegen, lautet wie folgt: Ich setze die Cursorfarbe im App-Delegierten auf Blau (-Anwendung: didFinishLaunchingWithOptions :):
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setTintColor:[UIColor blueColor]];
Verwenden Sie dann die Tönungsfarbe der Suchleiste in jedem Controller, um die Farbe der Schaltflächen festzulegen. Sie können die Farbtonfarbe auch im Storyboard einstellen.
Für Swift 3:
self.searchController.searchBar.tintColor = UIColor.white
Für Swift 4.2
let attributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(attributes, for: UIControl.State.normal)
Das Textattribut wird von IOS 7 nicht mehr verwendet, für IOS 7 oder höher
[[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont fontWithName:@"Helvetica" size:17]} forState:UIControlStateNormal];
Schnell 4
let uiButton = bar.value(forKey: "cancelButton") as? UIButton
uiButton?.setTitle("Cancel", for: .normal)
uiButton?.setTitleColor(UIColor.white,for: .normal)
Eine Alternative zur UISearchBar wäre die SHSearchBar . Dieses Swift-Framework ist ohne Hacking leicht anpassbar, Open Source, hat viele Unit-Tests und ist über Cocoapods verfügbar. Es benötigt mindestens iOS 8.
Für Leute, die Swift verwenden, musste ich zuerst hinzufügen Die Erweiterung von Eddie K
Dann konnte ich es so nennen (im Wesentlichen das, was Sabo in der akzeptierten Antwort tat):
UIBarButtonItem.appearanceWhenContainedWithin(UISearchBar.self).setTitleTextAttributes()