Search Bar Left Image Tint Change iOS for Dark Mode - ios

Trying to get the left most image icon within the search bar to change color for Dark Mode in iOS 13
Ive tried making a call directly to the searchBar and changing its background color, changing the text color, and finally I've tried a couple different iterations of the code below.
searchBar.backgroundColor = .clear
searchBar.backgroundImage = UIImage(named: "magnifyingglass")
if #available(iOS 13, *) {
searchBar.textField?.attributedPlaceholder =
NSAttributedString(string: (" " + NSLocalizedString("search_actions", comment: "")),
attributes: [NSAttributedString.Key.foregroundColor: UIColor.separator,
NSAttributedString.Key.strokeColor: UIColor.separator])
}

Related

NavigationBar color changes not carried through to other views with UINavigationBarAppearance()

I’m trying to work this out in Xcode 12.5.1 so I can move to Xcode 13. I have run this app in Xcode 13 Released and that’s how I discovered the problem. I know I’m a little late to the game but before Xcode 13… it wasn’t broken.
In my apps I have custom themes setup in three configurations, summer, fall and winter. Each theme sets the color attributes of the navBar as well as those of the rest of the controls in the app. The theme is chosen on the settings VC from a segmented control.
The setNavbarAttributes() code is run from AppDelegate in didFinishLaunchingWithOptions. It sets the correct navBar attributes when the app launches. In this case the code work as expected. I can verify this by navigating to the other views in the app and observing that the navBar shows the correct color attributes.
The setNavbarAttributes() code also runs when a theme is chosen from the segmented control on the settings VC. Here is the problem. When the theme is changed the navBar color attributes are not carried through to the navBar in the other views. Does anybody know why this isn’t working? I have a not so great workaround by putting the settings VC update code in an extension but that means touching every VC. That doesn't seem right.
It worked fine with my old code shown below but that’s broken in Xcode 13 with UINavigationBarAppearance().
let theNavebarProperties = UINavigationBar.appearance()
theNavebarProperties.barTintColor = Theme.current.navbarColor
theNavebarProperties.isTranslucent = false
theNavebarProperties.titleTextAttributes = [.foregroundColor: Theme.current.accentColor, .font: UIFont.systemFont(ofSize: gNavBarTitleTextSize, weight: .semibold)]
This sets the attributes of the navBar across the app.
class SetNarbar
{
static func setNavbarAttributes()
{
let theAppearance = UINavigationBarAppearance()
theAppearance.configureWithOpaqueBackground()
theAppearance.backgroundColor = Theme.current.navbarColor
theAppearance.titleTextAttributes = [.foregroundColor: Theme.current.accentColor, .font: UIFont.systemFont(ofSize: gNavBarTitleTextSize, weight: .semibold)]
UINavigationBar.appearance().standardAppearance = theAppearance
UINavigationBar.appearance().scrollEdgeAppearance = theAppearance
UINavigationBar.appearance().compactAppearance = theAppearance
}
}
This code is run in the settings VC on viewDidLoad and when the theme selector is tapped.
// Sets the controls on the settings VC to the currently selected theme. I have omitted code that works and does not pertain to setting the navBar.
func updateThemeOnSettingsVC()
{
setNeedsStatusBarAppearanceUpdate()
SetNarbar.setNavbarAttributes()
let navAppearance = UINavigationBarAppearance()
navAppearance.configureWithOpaqueBackground()
navAppearance.backgroundColor = Theme.current.navbarColor
navAppearance.titleTextAttributes = [.foregroundColor: Theme.current.accentColor, .font: UIFont.systemFont(ofSize: gNavBarTitleTextSize, weight: .semibold)]
navigationItem.standardAppearance = navAppearance
navigationItem.scrollEdgeAppearance = navAppearance
navigationItem.compactAppearance = navAppearance
}

How to change UIDocumentInteractionController text color?

My app has an option for a dark theme, and when that is the case, I do something like this:
UILabel.appearance().textColor = .white
The problem is, the app provides the ability to export some text via UIDocumentInteractionController. When I do presentPreview it displays white text on a white background, which makes the text completely invisible. There doesn't seem to be any way to change the text color or background color of the UIDocumentInteractionController.
I tried this
UILabel.appearance(whenContainedInInstancesOf: [UIDocumentInteractionController.self]).textColor = .black
but that just gives Cannot convert value of type 'UIDocumentInteractionController.Type' to expected element type 'UIAppearanceContainer.Type'
I've also tried setting UILabel.appearance().textColor = .black before I create the UIDocumentInteractionController, but that doesn't help either - the text still appears white.
What's extra annoying, this works to change the nav bar title color:
let navBarTitleTextAttributes = [
NSAttributedString.Key.foregroundColor: UIColor.black,
]
UINavigationBar.appearance().titleTextAttributes = navBarTitleTextAttributes
self.dic = UIDocumentInteractionController()
...
But the label appearance is not responsive.

How can I set the normal color for a floating placeholder in a MDCMultilineTextField?

I'm having troubles changing the color of the placeholder of a MDCMultilineTextField in the non-active state. I'm using a MDCTextInputControllerUnderline and I've managed to personalize every other part of the textfield, with the exception of the floating placeholder when not active (see pictures below, I need it to be white).
I've tried setting the tintColor of the textfield, the textColor and the tintColor of the placeholderLabel, the normalColor, inlinePlaceholderColor, floatingPlaceholderNormalColor and floatingPlaceholderActiveColor of the controller, but nothing seems to work. What am I missing? What's the attribute to change to set the color of the placeholder?
As suggested in the comment, it was a matter of setting the Attributed Placeholder instead of the regular one.
So, in the end, it was just a matter of doing this:
let stringAttr = [NSAttributedStringKey.foregroundColor: UIColor.white]
let attributedPlaceholder = NSAttributedString(string: placeholder, attributes: stringAttr)
textField.attributedPlaceholder = attributedPlaceholder

Change Alertview UIAlertController Buttons and Title color in iOS

I wanted to change my alert view title and button color as per my App theme and for this, i have found the many solutions as below:
alertController.view.tintColor = UIColor.red
But above code is changing only button title color not the Alert title.
Testing on iOS 11 using Xcode 9.1
Use below code to change Alert title:
alertController.setValue(NSAttributedString(string: "test", attributes: [NSAttributedStringKey.font : UIFont.boldSystemFont(ofSize: 15),NSAttributedStringKey.foregroundColor : UIColor.red]), forKey: "attributedTitle")

Navigation Bar Title background is white in iOS11

For some reason in iOS 11 the title label in navigation bar has a white background:
This is just a normal navigation controller and the title is being set the default way:
self.title = #"My Title";
This problem is only happening in iOS 11 and the screenshot was taken from the simulator, otherwise for previous iOS version this works fine.
Any suggestions as to how i can have a normal clear background label or remove the white background which is coming up for whatever reason?
Check if you have set the title text attributes correctly. You can set the title text attributes from the Storyboard or Programatically. Select Storyboard -> NavigationBar -> Go to attribute inspector -> Title Text Attributes
or in ViewController try setting it manually
Swift 4:
self.navigationController?.navigationBar.titleTextAttributes = [
NSAttributedStringKey.foregroundColor: UIColor.white,
NSAttributedStringKey.font: UIFont.systemFont(ofSize: 17, weight: .bold)
]
Swift 3:
self.navigationController?.navigationBar.titleTextAttributes = [
NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont.systemFont(ofSize: 17, weight: .bold)
]

Resources