How to change WKWebview Color to white - ios

I am using WKWebview.
I want to change it's background to .white.
i tried -
wkWebView.backgroundColor = .white
wkWebView.scrollView.backgroundColor = .white
view.backgroundColor = .white
But it's not showing white background.

Try the following:
set view background to white
view.backgroundColor = .white
set WKWebView background to non-opaque & clear
wkWebView.backgroundColor = .clear
wkWebView.opaque = false
This will essentially make the webview see-thru (at least until it loads its own HTML content) and the background view will be visible.
A similar trick will help with dark mode support to avoid the white flash that occurs, but set view.backgroundColor = .systemBackgroundColor

Not quite an answer (but not enough rep to comment).
What I usually do to debug is set very odd colours to things to see what is actually being set, e.g. yellow, pink, blue. This way you can assess if the colour change you're trying to make is working, as well as breakpoints to make sure the line is getting called :)
What worked for me was:
webView.backgroundColor = .white

Related

Is there a way I can change the background color of the whole input bar in Message Kit?

I wish to change the background color of the input accessory bar to a reddish color. In the example code for MessageKit, there isn't any to change the background color. And running this piece of code doesn't work:
messageInputBar.backgroundColor = .red
or
messageInputBar.backgroundView.backgroundColor = .red
In order to color the different parts on the InputBarAccessoryView, use these snippets:
Coloring the content view:
messageInputBar.contentView.backgroundColor = .red
Coloring the text input view:
messageInputBar.inputTextView.backgroundColor = .red
Coloring the full view:
messageInputBar.backgroundView.backgroundColor = .red
Have you tried this? :
messageInputBar.contentView.backgroundColor

Is UISegmentedControl just not usable currently?

Our application has a "dark" palette, with mostly black or charcoal backgrounds. This is creating a major problem in Apple controls that ignore (or don't even offer) control over text and background color.
UISegmentedControl is a particularly good example. It's drawn with often illegible, seemingly arbitrary text/background combinations. These controls are all set up with the exact same properties in IB, and yet you never know if they'll be legible from one view controller to the next.
Most of these are OK in "dark" mode in our app, but "light" mode is shambolic. I've spent a day experimenting with themes, UIAppearance, and setting appearance in IB and programmatically. I'm fed up with it. Does anyone actually know how to guarantee legibility in these things?
Try this,
let seg:UISegmentedControl = {
let seg = UISegmentedControl()
seg.insertSegment(withTitle: "tab 1", at: 0, animated: true)
seg.insertSegment(withTitle: "tab 2", at: 1, animated: true)
seg.selectedSegmentTintColor = .red //you can replace the colours you want
seg.backgroundColor = .lightGray //you can replace the colours you want
return seg
}()
Result
Dark Mode
Light Mode

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.

Having textfield background transparent, but placeholder text opaque

Is it possible to have a textfield's placeholder text opaque while its background is set to be transparent, eg. if I choose some background color, and set alpha to some value, like this:
the placeholder text becomes transparent as well. But I want it opaque. So, is this achievable, preferably using storyboard ? Or at least through the code.
If it is unclear what I am trying to achieve, just let me know , and I'll post an image with an example.
You can set the color's transparency instead of the UITextField's alpha. Open the color drop down and select "Other". A color picker will open up. At the bottom of the color picker you can change the opacity.
In Swift you can obtain the placeholder element with:
let placeHolder = textField.value(forKey: "placeholderLabel") as! UILabel
placeHolder.textColor = .blue
placeHolder.isOpaque = true
and make all customizations you prefeer..
You can set the UITextView.background = .clear
I'm providing a little more code so it'll be easier to setup it programmatically, this is a ready-to-use sample.
private lazy var textField: UITextView = {
let textField = UITextView()
textField.translatesAutoresizingMaskIntoConstraints = false
textField.font = UIFont.systemFont(ofSize: 14)
textField.isEditable = false
textField.textAlignment = .center
textField.isScrollEnabled = false
textField.backgroundColor = .clear // this is the line that answer you question
textField.text = """
People with these symptoms may have COVID-19:
Fever of chills, cough, shortness of breath or
difficulty breathing, fatigue, muscle or body aches,
headache, new loss of taste and smell, sore throat,
congestion or runny nose, nausea or vomiting,
diarrhea
"""
return textField
}()
Just remember to set the constraints and add the subview into your superview .addSubview(TextField)

UIImageView.appearance is overriding UISegmentedControl.appearance

I've been trying to use the appearance proxy API to apply some default colors to some controls, but I've run into a problem.
When I apply a tint color to UISegmentedControl using something like...
UISegmentedControl.appearance().tintColor = UIColor.red
It generates this...
All good, but when I add...
UIImageView.appearance().tintColor = UIColor.green
it changes to...
Just to be clear, I have BOTH this lines in my code
UISegmentedControl.appearance().tintColor = UIColor.red
UIImageView.appearance().tintColor = UIColor.green
It doesn't matter in what order I call them, the result is the same, the UIImageView properties override the UISegmentedControls
I've spent over half a day trying to find a solution to this problem but can't seem to find anything that works.
Running Xcode 8.2, iOS 10, Swift 3
What am I doing wrong and how can I fix?
I am not sure about this, but I guess, UISegmentedControl uses UIImageView to create segments, i.e. the segments we see inside segmented control are UIImageViews and not UIViews. UISegmentedControl even has methods to setImage for a particular segment.
If above is true, we can use appearanceWhenContainedIn API of UIAppearance to set image view tint colour like this:
UIImageView.appearance(whenContainedInInstancesOf: [UISegmentedControl.self]).tintColor = UIColor.red
UIImageView.appearance().tintColor = UIColor.green

Resources