As we know we change text color of SearchBar by this line of code and it works good:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.blue]
And the placeholder for color is UIColor.blue.
The questions is:
How to grab specific color?
I have tried to change UIColor.blue to UIColor.init(red: 200, green: 200, blue: 200, alpha: 1)
However no matter what number of red/green/blue/alpha i provide to init() i cannot grab a color.
Is there an Enum limitation like .blue/.red/.yellow?
What i am doing wrong?
public extension UISearchBar
{
public func SetSearchbarcolor(srchcolor: UIColor)
{
let Search = subviews.flatMap { $0.subviews }
guard let Searchfilter = (Search.filter { $0 is UITextField }).first as? UITextField else { return }
Searchfilter.textColor = srchcolor
}
}
Output : Searchbar_name.SetSearchbarcolor(srchcolor: UIColor.colorname)
Instead of UIColor.init, try
UIColor(red: 100/255, green: 100/255, blue: 100/255, alpha: 1)
Related
I am following this tutorial provided on Youtube for: How to Save Data with UserDefaults - Swift
https://www.youtube.com/watch?v=sUhq1vIrRbo
And I have this code that works for one page only and would like to know how to do the same exact thing (changing background color) but for my entire app pages based on the user's choice.
I have tried keeping the checkForStylePreference() in the viewDidLoad()of another page but it did not recognize it. I copy pasted the whole checkForStylePreference() but still other pieces of code were missing. Is the only way to do it is by copy pasting all of the methods of the viewController in all App pages? Or there is a much simpler way as a believe to reduce amount of code? Currently I can change BgColor from white to grey perfectly enter image description here but I don't know how to apply it for all.
This is the code of my NameViewController.swift (the one I've created for the page in the screenshot). Please note that I have 2 more swift files which are SAButton.swift and ConstantStyles.swift (for the colors)
class NameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
nameLbl.text = myString
checkForStylePreference()
}
#IBAction func didChangeStyleSeg(_ sender: UISegmentedControl) {
isDarkMode = sender.selectedSegmentIndex == 1
saveStylePreference()
updateStyle()
}
var myString = String()
#IBOutlet weak var styleSegment: UISegmentedControl!
#IBOutlet weak var nameLbl: UILabel!
var isDarkMode = false
let defaults = UserDefaults.standard
struct Keys {
static let preferDarkMode = "preferDarkMode"
}
func updateStyle(){
UIView.animate(withDuration: 0.4){
// self.view.backgroundColor = self.isDarkMode ? Colors.darkGrey : .white
// UIColor(hue: 287/360, saturation: 15/100, brightness: 85/100, alpha: 1.0)
self.view.backgroundColor = self.isDarkMode ? Colors.lightGrey : .white
//recent correct one
// self.view.backgroundColor = self.isDarkMode ? Colors.darkGrey : .white
//self.view.UIBackgroundFetchResult = self.isDarkMode? UIColor.grey : .white
}
}
func saveStylePreference(){
defaults.set(isDarkMode, forKey: Keys.preferDarkMode)
}
func checkForStylePreference(){
let preferDarkMode = defaults.bool(forKey: Keys.preferDarkMode)
if preferDarkMode{
isDarkMode = true
updateStyle()
styleSegment.selectedSegmentIndex = 1
}
}
// Do any additional setup after loading the view.
}
Code of the SAButton.swift
class SAButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
setupButton()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setupButton()
}
private func setupButton() {
setTitleColor(.white, for: .normal)
backgroundColor = Colors.lightBlue
titleLabel?.font = .boldSystemFont(ofSize: 20)
layer.cornerRadius = frame.size.height / 2
}
}
Code of the ConstantStyles.swift
import UIKit
struct Colors {
static let darkGrey = UIColor(red: 40/255, green: 40/255, blue: 40/255, alpha: 1)
// static let purple = UIColor(red: 212/255, green: 186/255, blue: 86/255, alpha: 1)
static let lightBlue = UIColor(red: 89/255, green: 205/255, blue: 242/255, alpha: 1)
static let darkPurple = UIColor(red: 242/255, green: 232/255, blue: 255/255, alpha: 1.0)
// UIColor(hue: 287/360, saturation: 15/100, brightness: 85/100, alpha: 1.0)
static let lightPurple = UIColor(red: 240/255, green: 229/255, blue: 255/255, alpha: 1.0)
static let lightGrey = UIColor(red: 237/255, green: 237/255, blue: 237/255, alpha: 1.0)
//UIColor(red: 249/255, green: 244/255, blue: 255/255, alpha: 1.0)
}
I believe it could be simple but I am new to Swift, I would like to know what part of code to keep exactly and where. Much appreciated.
Ps: Original project Source Code is provided below the Youtube Video.
You can create a main class and inherit from it
class GeneralVC : UIViewController {
override func viewDidLoad() {
self.view.backgroundColor = .red // read color from userdefaults and set it here
}
}
class ViewController: GeneralVC {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
Same applies to any UIKit component that you need to affect globally
Another interesting way to do it is to use Appearance:
Perhaps you can use UIViewControllerWrapperView as a parent.
UIView.appearance(whenContainedInInstancesOf: [UIViewControllerWrapperView]) // UIViewControllerWrapperView might be private. In that case it might take some wizardry to get it to work
Another way to do it is to set it when the UITabBarController or UINavigationController presents a new UIViewController. You can do this by subclassing them.
The reason why I don't like subclassing is that you force a subclass for just one simple thing. If you only do it in a few navigation based ones it's much easier and also easier to override with extensions instead of everything through subclassing.
I have the following subclass:
class GeneralNavigationBar: UINavigationBar {
override func layoutSubviews() {
super.layoutSubviews()
self.barTintColor = UIColor(rgb: 0x2A5298) //Extension that converts hex to color
self.tintColor = UIColor.white
}
}
I want the title of the bar to be white. When I apply this class to a NavigationBar in the storyboard, the background gets blue (hex, as he is supposed to), but the title remains black.
It's strange, since you can alter the color of the bar in the ViewController it appears in:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0)
self.navigationController?.navigationBar.tintColor = UIColor.whiteColor()
This works.
Try appearance,see how..
var navbarappearace = UINavigationBar.appearance()
navbarappearace.barTintColor = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0)
navbarappearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
Put the above code in AppDelegate, it should affect the whole project.
Cheers.
I made a push function in a button:
#IBAction func prodButton(sender: AnyObject) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var secondViewController = CollectionViewController()
secondViewController = storyboard.instantiateViewControllerWithIdentifier("CollectionViewController") as! CollectionViewController
self.navigationController?.pushViewController(secondViewController, animated: true)
}
This button pushes to the secondViewController but when I looked at the navigation bar of the second view controller I noticed that it has set up a back button automatically. The problem is that this back button's color is light blue and it doesn't fit with my design. I tried to change it like that in viewDidAppear
self.navigationItem.leftBarButtonItem?.tintColor = UIColor.redColor()
and also the bar color:
self.navigationController?.navigationBar.barTintColor = UIColor(red: 65, green: 61, blue: 116, alpha: 1.0)
but there wasn't any change.
I'd be really thankful if somebody help me with that.
Thanks in advance.
Try to set your color one time, it will be the same color for everywhere after:
let navBar = UINavigationBar.appearance()
navBar.tintColor = UIColor.redColor()
You should put this code in AppDelegate. You could also set the barTint in Storyboard if you use it.
Use
//Func Alter color navigation bar
func AlteraCorNavigationBar(Navigation:UINavigationController){
Navigation.navigationBar.tintColor = CorTextoNavigationBar()
//Navigation.navigationBar.barTintColor = CorPredominante()
Navigation.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : CorTextoNavigationBar(), NSFontAttributeName : UIFont(name: "ArialHebrew-Light", size: 22)!]
}
//func CorPredominante()->UIColor{
//return UIColor.redColor()
//(rgba: "#ecf0f1")
// YOU CAN LIBRARY COLOR RGBA [HERE][1] AND USE UIColor(rgba: "#b6011a")
//}
func CorTextoNavigationBar()->UIColor{
return UIColor(red: 65, green: 61, blue: 116, alpha: 1.0)
}
for called in view controller:
class NewViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
AlteraCorNavigationBar(navigationController)
}
}
I found the mistake.
The colors shoud be devided by 255:
UINavigationBar.appearance().tintColor = UIColor(red: 149.0 / 255.0, green: 148.0 / 255.0, blue: 192.0 / 255.0, alpha: 0.5)
UINavigationBar.appearance().barTintColor = UIColor(red: 65.0 / 255.0, green: 61.0 / 255.0, blue: 116.0 / 255.0, alpha: 1.0)
Thank you all for the help ;)
I'm trying to implement a standard universal styling of my text fields based strictly on the login/sign up fields.
So, I designed them all to be identical, but I think that I'm reusing a lot of code that can be condensed and maybe used in a variable? I'm not sure how to do so.
The way it is works, but i'm sure it can be done better than this. I'm almost certain there's a way to minimize this code for better practice.
I'm still learning, so I really want to learn better practice in dev.
Here's an example of my sign up view & the styling of the fields:
class JoinVC: UIViewController, UITextFieldDelegate {`
#IBOutlet weak var enterEmailTextField: UITextField!
#IBOutlet weak var enterPasswordTextField: UITextField!
#IBOutlet weak var enterNameTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Field Border Corner + Width
self.enterEmailTextField.layer.cornerRadius = 24.0
self.enterEmailTextField.layer.borderWidth = 1.5
self.enterPasswordTextField.layer.cornerRadius = 24.0
self.enterPasswordTextField.layer.borderWidth = 1.5
self.enterNameTextField.layer.cornerRadius = 24.0
self.enterNameTextField.layer.borderWidth = 1.5
// ...
// Field Placeholder ColorEnter
var placeholderEnterEmail = NSAttributedString(string: "Enter Email", attributes: [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)])
var placeholderEnterPass = NSAttributedString(string: "Choose Password", attributes: [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)])
var placeholderEnterName = NSAttributedString(string: "Choose Username", attributes: [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)])
enterEmailTextField.layer.sublayerTransform = CATransform3DMakeTranslation(20, 0, 0);
enterPasswordTextField.layer.sublayerTransform = CATransform3DMakeTranslation(20, 0, 0);
enterNameTextField.layer.sublayerTransform = CATransform3DMakeTranslation(20, 0, 0);
// ...
// Text Field Border color
var borderColor : UIColor = UIColor( red: 255, green: 255, blue:255, alpha: 0.8 )
self.enterPasswordTextField.layer.borderColor = borderColor.CGColor; enterEmailTextField.attributedPlaceholder = placeholderEnterEmail;
self.enterEmailTextField.layer.borderColor = borderColor.CGColor; enterPasswordTextField.attributedPlaceholder = placeholderEnterPass;
self.enterNameTextField.layer.borderColor = borderColor.CGColor; enterNameTextField.attributedPlaceholder = placeholderEnterName;
// ...
}
}
The way I have solved this before, especially if all text fields in the project are:
Always going to have identical attributes,
Always going to be created from the storyboard
is to subclass UITextField and apply the attributes in -awakeFromNib:
class KBTextField: UITextField {
let myAttributes = [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)]
let mySublayerTransform = CATransform3DMakeTranslation(20, 0, 0)
let myBorderColor = UIColor( red: 255, green: 255, blue:255, alpha: 0.8 )
override func awakeFromNib () {
self.layer.sublayerTransform = mySublayerTransform
self.layer.borderColor = myBorderColor.CGColor
self.layer.cornerRadius = 24.0
self.layer.borderWidth = 1.5
self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: myAttributes)
}
}
Then you can just set the class right on the storyboard (to KBTextField, in this case) and it will take care of all your attributes automatically.
This way, you can ensure that all KBTextFields in your app look identical, as long as you create them through the storyboard.
easy solution for reusable code is a handler
func setupTextField(textField : UITextField, placeHolderString: String)
{
let borderColor = UIColor( red: 1.0, green: 1.0, blue:1.0, alpha: 0.8 )
textField.layer.cornerRadius = 24.0
textField.layer.borderWidth = 1.5
textField.layer.sublayerTransform = CATransform3DMakeTranslation(20, 0, 0);
textField.layer.borderColor = borderColor.CGColor;
textField.attributedPlaceholder = NSAttributedString(string: placeHolderString, attributes: [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)])
}
override func viewDidLoad() {
super.viewDidLoad()
setupTextField(enterEmailTextField, placeHolderString: "Enter Email")
setupTextField(enterPasswordTextField, placeHolderString: "Choose Password")
setupTextField(enterNameTextField, placeHolderString: "Choose Username")
}
Note: UIColor values must be in the range of 0.0 to 1.0
You can use the Extension functionality.
Just create an Extension of UITextField an place your code into that Extension
(easy tutorial here)
Ex:
extension UITextField
{
func setPlaceholderColorAndString(pholder: String, color: UIColor)
{
self.attributedPlaceholder = NSAttributedString(string:pholder,
attributes:[NSForegroundColorAttributeName: color])
}
func setupField()
{
self.layer.cornerRadius = 24.0
self.layer.borderWidth = 1.5
}
}
Then in your code you can use it like:
self.enterEmailTextField.setPlaceholderColorAndString(pholder: "Enter Email", color: UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)
self.enterEmailTextField.setupField()
self.enterPasswordTextField.setPlaceholderColorAndString(pholder: "Enter Password", color: UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)
self.enterPasswordTextField.setupField()
And so on. Of course the above snippet it's only to give you an idea how using it, you can improve it a lot to fit your needs.
I want to change the text and the background color of the view on tap. The quotes are changing, but the view background color don't change. What am I doing wrong?
Here is the code:
import UIKit // UI: user interface
class ViewController: UIViewController {
// IB: Interface Builder
#IBOutlet weak var quoteLabel: UILabel!
var quotes = Quotes()
// gets called when the view is loaded
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
// Interface Builder Action.
// Gets called whenever the user taps on the button
#IBAction func inspireMeDidTap(sender: UIButton)
{
let quote = quotes.randomQuote()
quoteLabel.text = quote
// change the background color of the view
view.backgroundColor = randomColor()
}
func randomColor() -> UIColor
{
let random = Int(arc4random()) % 5 // 0 -> 4
switch random {
case 0: return UIColor(red: 211/255.0, green: 86/255.0, blue: 87/255.0, alpha: 0)
case 1: return UIColor(red: 71/255.0, green: 178/255.0, blue: 137/255.0, alpha: 0)
case 2: return UIColor(red: 229/255.0, green: 177/255.0, blue: 93/255.0, alpha: 0)
case 3: return UIColor(red: 92/255.0, green: 163/255.0, blue: 178/255.0, alpha: 0)
case 4: return UIColor(red: 38/255.0, green: 38/255.0, blue: 38/255.0, alpha: 0)
default: return UIColor(red: 56/255.0, green: 72/255.0, blue: 72/255.0, alpha: 0)
}
}
}
try this:
put all alpha = 1 in your colors (instead of 0).
If alpha = 0, entirely transparent
Meaning of alpha
You are setting alpha parameter to zero it means whatever the color is it will be completely transperent(not white).
So what you need to do is just set alpha to 1 and you will get your correct color