Ich verwende eine Auswahlansicht, damit der Benutzer das Farbschema für die gesamte App auswählen kann. Ich plane, die Farbe der Navigationsleiste, des Hintergrunds und möglicherweise der Registerkartenleiste zu ändern (sofern dies möglich ist). Ich habe nachgeforscht, wie das geht, kann aber keine Swift Beispiele finden. Kann mir jemand bitte ein Beispiel für den Code geben, den ich zum Ändern der Farbe der Navigationsleiste und der Textfarbe der Navigationsleiste verwenden müsste? (Die Picker-Ansicht ist eingerichtet. Ich suche nur nach dem Code, um die Farben der Benutzeroberfläche zu ändern.)
Vielen Dank.
Navigationsleiste:
navigationController?.navigationBar.barTintColor = UIColor.green
Ersetzen Sie greenColor durch die von Ihnen gewünschte UIColor. Sie können auch RGB verwenden, wenn Sie dies vorziehen.
Navigationsleistentext:
navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
Ersetzen Sie orangeColor durch eine beliebige Farbe.
Tab-Leiste:
tabBarController?.tabBar.barTintColor = UIColor.brown
Text der Registerkartenleiste:
tabBarController?.tabBar.tintColor = UIColor.yellow
Ersetzen Sie in den letzten beiden Fällen brownColor und yellowColor durch die Farbe Ihrer Wahl.
Hier sind einige grundlegende Anpassungen des Erscheinungsbilds, die Sie app-weit anwenden können:
UINavigationBar.appearance().backgroundColor = UIColor.greenColor()
UIBarButtonItem.appearance().tintColor = UIColor.magentaColor()
//Since iOS 7.0 UITextAttributeTextColor was replaced by NSForegroundColorAttributeName
UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
UITabBar.appearance().backgroundColor = UIColor.yellowColor();
Weitere Informationen zur UIAppearance
API in Swift finden Sie hier: https://developer.Apple.com/documentation/uikit/uiappearance
UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
Fügen Sie diese Zeile einfach in didFinishLaunchingWithOptions
in Ihren Code ein.
Aktualisiert für Swift 3, 4 & 4.2
// setup navBar.....
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Schnell 4
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Swift 4.2
UINavigationBar.appearance().barTintColor = .black
UINavigationBar.appearance().tintColor = .white
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
UINavigationBar.appearance().isTranslucent = false
Kann auch hier nachsehen: https://github.com/hasnine/iOSUtilitiesSource
Innerhalb von AppDelegate hat dies das Format der Navigationsleiste global geändert und die unterste Zeile/den unteren Rand (der für die meisten Menschen ein Problem darstellt) entfernt, um Ihnen das zu geben, wonach Sie und andere suchen:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barTintColor = Style.SELECTED_COLOR
UINavigationBar.appearance().translucent = false
UINavigationBar.appearance().clipsToBounds = false
UINavigationBar.appearance().backgroundColor = Style.SELECTED_COLOR
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : (UIFont(name: "FONT NAME", size: 18))!, NSForegroundColorAttributeName: UIColor.whiteColor()] }
Anschließend können Sie eine Constants.Swift -Datei einrichten. Enthalten ist eine Style-Struktur mit Farben und Schriftarten usw. Sie können dann jedem ViewController eine tableView/pickerView hinzufügen und das Array "availableThemes" verwenden, um es dem Benutzer zu ermöglichen ThemeColor ändern.
Das Schöne daran ist, dass Sie für jede Farbe eine Referenz in Ihrer gesamten App verwenden können. Diese wird basierend auf dem ausgewählten "Theme" des Benutzers aktualisiert. Ohne diese wird standardmäßig theme1 () verwendet:
import Foundation
import UIKit
struct Style {
static let availableThemes = ["Theme 1","Theme 2","Theme 3"]
static func loadTheme(){
let defaults = NSUserDefaults.standardUserDefaults()
if let name = defaults.stringForKey("Theme"){
// Select the Theme
if name == availableThemes[0] { theme1() }
if name == availableThemes[1] { theme2() }
if name == availableThemes[2] { theme3() }
}else{
defaults.setObject(availableThemes[0], forKey: "Theme")
theme1()
}
}
// Colors specific to theme - can include multiple colours here for each one
static func theme1(){
static var SELECTED_COLOR = UIColor(red:70/255, green: 38/255, blue: 92/255, alpha: 1) }
static func theme2(){
static var SELECTED_COLOR = UIColor(red:255/255, green: 255/255, blue: 255/255, alpha: 1) }
static func theme3(){
static var SELECTED_COLOR = UIColor(red:90/255, green: 50/255, blue: 120/255, alpha: 1) } ...
Um dies im Storyboard zu tun (Interface Builder Inspector)
Mit Hilfe von IBDesignable
können wir dem Interface Builder Inspector weitere Optionen für UINavigationController
hinzufügen und diese im Storyboard optimieren. Fügen Sie Ihrem Projekt zunächst den folgenden Code hinzu.
@IBDesignable extension UINavigationController {
@IBInspectable var barTintColor: UIColor? {
set {
guard let uiColor = newValue else { return }
navigationBar.barTintColor = uiColor
}
get {
guard let color = navigationBar.barTintColor else { return nil }
return color
}
}
}
Stellen Sie dann einfach die Attribute für den Navigationscontroller im Storyboard ein.
Dieser Ansatz kann auch verwendet werden, um die Farbe des Navigationsleistentexts im Storyboard zu verwalten:
@IBInspectable var barTextColor: UIColor? {
set {
guard let uiColor = newValue else {return}
navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: uiColor]
}
get {
guard let textAttributes = navigationBar.titleTextAttributes else { return nil }
return textAttributes[NSAttributedStringKey.foregroundColor] as? UIColor
}
}
UINavigationBar.appearance().barTintColor
arbeitete für mich
Swift 4:
Perfekt funktionierender Code zum Ändern des Erscheinungsbilds der Navigationsleiste auf Anwendungsebene.
// MARK: Navigation Bar Customisation
// To change background colour.
UINavigationBar.appearance().barTintColor = .init(red: 23.0/255, green: 197.0/255, blue: 157.0/255, alpha: 1.0)
// To change colour of tappable items.
UINavigationBar.appearance().tintColor = .white
// To apply textAttributes to title i.e. colour, font etc.
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white,
.font : UIFont.init(name: "AvenirNext-DemiBold", size: 22.0)!]
// To control navigation bar's translucency.
UINavigationBar.appearance().isTranslucent = false
Viel Spaß beim Codieren!
Swift 4 - Glatter Übergang (beste Lösung):
Wenn Sie sich von einem Navigationscontroller zurückbewegen und eine andere Farbe für den Navigationscontroller festlegen müssen, den Sie gedrückt haben, möchten Sie ihn verwenden
override func willMove(toParentViewController parent: UIViewController?) {
navigationController?.navigationBar.barTintColor = .white
navigationController?.navigationBar.tintColor = Constants.AppColor
}
anstatt es in viewWillAppear zu platzieren, wird der Übergang sauberer.
Swift 4.2
override func willMove(toParent parent: UIViewController?) {
navigationController?.navigationBar.barTintColor = UIColor.black
navigationController?.navigationBar.tintColor = UIColor.black
}
Verwenden Sie die Darstellungs-API und die Farbe barTintColor.
UINavigationBar.appearance().barTintColor = UIColor.greenColor()
In Swift 4
Sie können die Farbe der Navigationsleiste ändern. Verwenden Sie einfach das folgende Code-Snippet in viewDidLoad()
Farbe der Navigationsleiste
self.navigationController?.navigationBar.barTintColor = UIColor.white
Textfarbe der Navigationsleiste
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.purple]
Für iOS 11 Large Title Navigation Bar müssen Sie die Eigenschaft largeTitleTextAttributes
verwenden
self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.purple]
Die appear () Funktion funktioniert bei mir nicht immer. Deshalb ziehe ich es vor, ein NC-Objekt zu erstellen und dessen Attribute zu ändern.
var navBarColor = navigationController!.navigationBar
navBarColor.barTintColor = UIColor(red: 255/255.0, green: 0/255.0, blue: 0/255.0, alpha: 100.0/100.0)
navBarColor.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
Auch wenn Sie ein Bild anstelle von nur Text hinzufügen möchten, funktioniert dies ebenfalls
var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
imageView.contentMode = .ScaleAspectFit
var image = UIImage(named: "logo")
imageView.image = image
navigationItem.titleView = imageView
Wenn Sie einen benutzerdefinierten Navigationscontroller haben, können Sie den obigen Codeausschnitt verwenden. In meinem Fall habe ich folgende Codestücke verwendet.
Swift 3.0, XCode 8.1 Version
navigationController.navigationBar.barTintColor = UIColor.green
Navigationsleisten-Text:
navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
Es sind sehr hilfreiche Gespräche.
Swift 4, iOS 12 und Xcode 10 Update
Setzen Sie einfach eine Zeile in viewDidLoad()
navigationController?.navigationBar.barTintColor = UIColor.red
iOS 8 (Swift)
let font: UIFont = UIFont(name: "fontName", size: 17)
let color = UIColor.backColor()
self.navigationController?.navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName: color], forState: .Normal)
UINavigationBar.appearance().barTintColor = UIColor(colorLiteralRed: 51/255, green: 90/255, blue: 149/255, alpha: 1)
Dadurch wird die Farbe Ihrer Navigationsleiste wie bei Facebook festgelegt. :)
Swift 3 und Swift 4 Compatible Xcode 9
Eine bessere Lösung, um eine Klasse für allgemeine Navigationsleisten zu erstellen
Ich habe 5 Controller und jeder Controllertitel wird in orange geändert. Da jeder Controller 5 Navigations-Controller hat, musste ich jede einzelne Farbe entweder vom Inspektor oder vom Code aus ändern.
Also habe ich eine Klasse erstellt, anstatt jede einzelne Navigationsleiste von Code zu ändern. Ich habe nur diese Klasse zugewiesen und es hat bei allen 5 Controller-Code-Wiederverwendungsfähigkeiten funktioniert.Sie müssen nur diese Klasse jedem zuweisen.) controller und das wars.
import UIKit
class NabigationBar: UINavigationBar {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonFeatures()
}
func commonFeatures() {
self.backgroundColor = UIColor.white;
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor:ColorConstants.orangeTextColor]
}
}
In Swift 2
Zum Ändern der Farbe in der Navigationsleiste
navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
Zum Ändern der Farbe in der Elementnavigationsleiste
navigationController?.navigationBar.tintColor = UIColor.blueColor()
oder
navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.blueColor()]
iOS 10 Swift 3.0
Wenn es Ihnen nichts ausmacht, Swift Frameworks zu verwenden, dann verwenden Sie INeraida , um den Navigationshintergrund als UIColor
oder HexColor
oder UIImage
zu ändern und zu ändern Navigationstaste Text programmgesteuert zurück, vollständige Hintergrundfarbe ändern.
Für UINavigationBar
neraida.navigation.background.color.hexColor("54ad00", isTranslucent: false, viewController: self)
//Change navigation title, backbutton colour
neraida.navigation.foreground.color.uiColor(UIColor.white, viewController: self)
//Change navigation back button title programmatically
neraida.navigation.foreground.backButtonTitle("Custom Title", ViewController: self)
//Apply Background Image to the UINavigationBar
neraida.navigation.background.image("background", Edge: (0,0,0,0), barMetrics: .default, isTranslucent: false, viewController: self)
Swift
Einfacher Einzeiler, den Sie in ViewDidLoad()
verwenden können
//Change Color
self.navigationController?.navigationBar.barTintColor = UIColor.red
//Change Text Color
self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
Ich musste es tun
UINavigationBar.appearance().tintColor = UIColor.whiteColor()
UINavigationBar.appearance().barStyle = .Black
UINavigationBar.appearance().backgroundColor = UIColor.blueColor()
andernfalls würde sich die Hintergrundfarbe nicht ändern
Setzen Sie zuerst die Eigenschaft isTranslucent von navigationBar auf false, um die gewünschte Farbe zu erhalten. Dann ändere die Farbe der Navigationsleiste wie folgt:
@IBOutlet var NavigationBar: UINavigationBar!
NavigationBar.isTranslucent = false
NavigationBar.barTintColor = UIColor (red: 117/255, green: 23/255, blue: 49/255, alpha: 1.0)
Stellen Sie sicher, dass Button State for .normal eingestellt ist
extension UINavigationBar {
func makeContent(color: UIColor) {
let attributes: [NSAttributedString.Key: Any]? = [.foregroundColor: color]
self.titleTextAttributes = attributes
self.topItem?.leftBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
self.topItem?.rightBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
}
}
P.S iOS 12, Xcode 10.1
Versuchen Sie dies in AppDelegate:
//MARK:- ~~~~~~~~~~setupApplicationUIAppearance Method
func setupApplicationUIAppearance() {
UIApplication.shared.statusBarView?.backgroundColor = UIColor.clear
var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
UINavigationBar.appearance().tintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
UINavigationBar.appearance().barTintColor = UIColor.white
UINavigationBar.appearance().isTranslucent = false
let attributes: [NSAttributedString.Key: AnyObject]
if DeviceType.IS_IPAD{
attributes = [
NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "HelveticaNeue", size: 30)
] as [NSAttributedString.Key : AnyObject]
}else{
attributes = [
NSAttributedString.Key.foregroundColor: UIColor.white
]
}
UINavigationBar.appearance().titleTextAttributes = attributes
}