Swift how to remove uisegmentControl default black backgroundColor? - swift

I know a lot of people asked this question, but nothing helps. My controller1 presents a new controller2 from storyboard. Controler1's background color is black. My customized segmentcontrol background color and selectedTintColor work well, but always show a black background color first. I tried every way, but not working. In storyboard and code, all no this black color. Thanks!
override func viewDidLoad() {
let bgColor = uIColor. // my customized color)
segmentControl.backgroundColor = bgColor
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
connectionSegmentControl.selectedSegmentIndex = 0
toggleSegmentTintColor()
}
private func toggleSegmentTintColor() {
// I set my customized selected tint color here, it works.
}

If you do this in viewDidAppear, its too late: the view is on the screen. The hack way to do it is in viewDidlaod or viewWillAppear. The right way to do it is to to use UIAppearance proxy on app launch to set the tint color for your object globally:
UISegmentedControl.appearance().tintColor = UIColor.clear
You can also set the tint color for the whole app by setting it on the root window, but then it affects more than just the segmented controls.

Related

borderColor of a UIButton doesn't respect overrideUserInterfaceStyle

I ran into an issue I'm having trouble explaining. I have a simple UIButton for which I set the following properties:
class SecondaryButton: AppButton {
override func awakeFromNib() {
super.awakeFromNib()
cornerRadius = frame.height / 2
borderWidth = 2
borderColor = .formedText.withAlphaComponent(0.2)
}
}
.formedText access a namedColor that is supposed to be black in light mode and white in dark mode (I am not using .label because that isn't pure white or pure black on macOS Catalyst).
static let formedText = UIColor(named: "Formed Text")
This button is used in a page where I tried overriding the userInterfaceStyle in several places by calling overrideUserInterfaceStyle = .light
on the entire window
on the view controller
on the button itself
So here's the question: As you can see the button has a white border, even though the named colour is supposed to be black when userInterfaceStyle is light. If I set the exact same colour as the background of the same button by calling backgroundColor = .formedText.withAlphaComponent(0.2) it works just fine (i.e. the background is black with alpha 0.2).
Setting the color to something not affected by dark mode (such as .black or .red) works fine.
What am I missing here?
Please let me know if any further details are needed, and thanks in advance for any hints.
borderColor is a cgColor which is not adaptive.
You can handle the appearance change and update the border color inside the traitCollectionDidChange function:
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if traitCollection.hasDifferentColorAppearance(comparedTo: previousTraitCollection) {
// Set the color again here if needed 👈
}
}
More information here

Erase borders between navigation bar and searchBar swift 4

I am setting both the navigation bar and the search bar to a custom UIColor (which I call categoryColor in my code). When I do that, I still see an upper grayish line between nav bar and search bar. I have already set the searchBar border color to be the same as the others, but that gray line still exists. Does anyone know how to get rid of it?
Here is my code:
override func viewWillAppear(_ animated: Bool) {
//defining the color that will be used for all the items
let categoryColor = UIColor(hexString: selectCategory?.categoryColorHex ?? UIColor.randomFlat.hexValue())
//changing navigation bar tint color
navigationController?.navigationBar.barTintColor = categoryColor
//changing searchbar tint color
searchBar.barTintColor = categoryColor
//change searchBar border's color
searchBar.layer.borderColor = categoryColor?.cgColor
searchBar.layer.borderWidth = 3
//changing title that appears at the top, after list is loaded
title = selectCategory?.listName ?? "Todoey"
}
Here is a picture of what I see when I run the simulation:
The better way for implementing search bar with nav controller would be to use searchController inside a navigationController so the searchController will have the same background as navigationController. Here’s a great tutorial about that: https://m.youtube.com/watch?v=h7caxpp3Xps
Edit:
Also if you already implemented search capabilities you can do that with searchController too. Just set navigationController.searchConroller.searchBar.delegate for class that’s responsible for handling delegate methods

How can I change the background color of the arrow in an iOS popover in Swift?

When I set the background color of a UIView in the storyboard to a specific color (e.g. black) the arrow of the popup remains unchanged (e.g. white).
How can I change the color of the arrow too?
If you want to see the arrow of a popover in the same color like the background color of the popover, add the following line in the "viewDidLoad()" method in your UIViewController (the "popup"):
override func viewDidLoad() {
super.viewDidLoad()
self.popoverPresentationController?.backgroundColor = self.view.backgroundColor
}
iOS popover with changed background color

Cannot get transparent Tab Bar in tvOS

I'm having a hard time getting a UITabBar with a completely transparent background on tvOS : I Always get a whitish background.
I tried the following (Setting background color clear, background image to UIImage) but nothing worked.
var appearanceTabBar = UITabBar.appearance()
appearanceTabBar.barTintColor = UIColor.clear
appearanceTabBar.shadowImage = UIImage()
appearanceTabBar.backgroundImage = UIImage()
Result is the following :
does anybody have any idea on how i can make the background completely transparent ?
Setting the color as the same purple is not a good idea as a picture may be on the background.
Thank you :)
Mickael
I was having the issue, you have to wait for the tab bar UI to be "ready". I solved it inheriting from UITabBarController and overriding viewWillAppear.
override func viewWillAppear(_ animated: Bool)
{
super.viewWillAppear(animated)
self.tabBar.backgroundImage = UIImage()
}
I guess you don't have to extend from UITabBarController, you probably can achieve the same result just overriding viewWillAppear on your main view controller.

Why I cannot make my UITabBarController blurred?

I have a UITabBar and I want to make it blurred. I wrote the following code:
import UIKit
class TabBarController:UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let blur = UIBlurEffect(style: UIBlurEffectStyle.Light)
let blurView = UIVisualEffectView(effect: blur)
blurView.frame = self.view.bounds
blurView.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
self.view.layer.insertSublayer(blurView, atIndex: 0)
}
}
but somehow the last line throws error:
Cannot convert value of type 'UIVisualEffectView' to expected argument
type 'CALayer'
how can I fix that?
I changed the last line to:
self.tabBar.addSubview(blurView)
but now the whole tabbar is blurred (even with icons and they are not visible). When I changed this line to:
self.tabBar.sendSubviewToBack(blurView)
then the tabbar is visible, but not blurred. I want to achieve effect from accepted answer from here Black background on transparent UITabBar but here it is uitabbar and I'm using uitabbarcontroller... Can you help me with applying blur in my case?
You just add the blur view as a subview:
self.view.addSubview(blurView)
Since you just want to blue the tab bar and this class is a tab bar controller, you can do:
self.tabBar.addSubview(blueView)
You also need to change the frame:
blurView.frame = self.tabBar.bounds
why don't you just use the barTintColor property on your TabBarController?
self.tabBar.translucent = true
self.tabBar.barTintColor = UIColor.blackColor()
You don't even need to subclass UITabBarController. You can call this on any UIViewController.
self.tabBarController?.tabBar.translucent = true
self.tabBarController?.tabBar.barTintColor = UIColor.blackColor()
If I understood correctly from the following comment that you posted, you want to change the UITabBar to be black in colour but still blurred.
And yes, I noticed that the UITabBarController is blurred by default, but I would like to make it blurred with specific style (.Dark).
Doing this since iOS 7 has actually become quite easy. Simply change the barStyle of your UITabBar to .black. Put the following code in your UIViewController's viewDidLoad method (note that UITabBar is translucent by default, so you don't need to specify that again).
tabBarController?.tabBar.barStyle = .black
If you want to set it back to the regular, white barStyle, change it back to .default.
tabBarController?.tabBar.barStyle = .default
You may even do this from within Interface Builder by selecting the Tab Bar in your UITabBarController's hierarchy and changing its Style to Black.
I have a solution, all you need is configure your UITabBar as following:
// next code will make tabBar fully transparent
tabBar.isTranslucent = true
tabBar.backgroundImage = UIImage()
tabBar.shadowImage = UIImage() // add this if you want remove tabBar separator
tabBar.barTintColor = .clear
tabBar.backgroundColor = .black // here is your tabBar color
tabBar.layer.backgroundColor = UIColor.clear.cgColor
If you want to add blur, do this:
let blurEffect = UIBlurEffect(style: .dark) // here you can change blur style
let blurView = UIVisualEffectView(effect: blurEffect)
blurView.frame = tabBar.bounds
blurView.autoresizingMask = .flexibleWidth
tabBar.insertSubview(blurView, at: 0)
As a result:
Attach bottom constraint to the bottom of the view instead of Safe Area
It just might not be a problem with your TabBar but with tableView constraints.
Tab bar is blurred by default.