Text disappearing when I add gradient background to UIPicker in Swift 3 - ios

I am trying to add a gradient background to my picker in the same way that I have added a gradient background to my VC.
When I do this via the code below, the gradient is added but the text disappears from view. Although the picker still behaves as if the text is there.
My guess is it's something to do with the .insertSublayer placing the gradient background over the top off the text.
override func viewDidLoad() {
let background = CAGradientLayer().bespokeColor()
background.frame = self.view.bounds
self.view.layer.insertSublayer(background, at: 0)
let pickerBackground = CAGradientLayer().bespokeColor()
pickerBackground.frame = self.Picker.bounds
self.Picker.layer.insertSublayer(pickerBackground, at: 0)
}
How do I get around this?

Related

UIView backgroundColor still white, after changing to clear

In my app I have a ViewController with 2 separated UIViews, but they both have the same frame and positions. Their background color is clear. I take screenshot of each of them and after look at pixel data of each UIImage. When I take screenshot of second UIView, I change backgrond color of UIView to white, because otherwise they overlap each other. At this moment all work correct, after I return background color to clear.
But when I try to repeat call my function again, first UIView has white pixels inside, like previous UIView still doesn't change background color.
I'm changing background color with:
secondView.backgroundColor = nil // also with = .clear
Here is my method to take screenshot:
func takeScreenshot(_ targetUIView: UIView,completion: #escaping(_ image: UIImage) ->Void) {
DispatchQueue.main.async() {
UIGraphicsBeginImageContextWithOptions(targetUIView.bounds.size, targetUIView.isOpaque, UIScreen.main.scale)
targetUIView.drawHierarchy(in: targetUIView.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
DispatchQueue.main.async() {
if (image != nil){
completion(image!)
}
}
}
}
What I tried:
Tried to hide view, where I change background color.
Tried to remove it from superview.
Tried to use setNeedsDisplay and setNeedsLayout methods.
Tried to change view in hierarchy with bringSubviewToFront and sendSubviewToBack methods.
All of this was useless for me, somehow it look like my second UIView still has white background color, even I change it to clear.

Background image in Swift

I am creating an application with a fixed background (2208x2208) for every screen, that is why I am setting the background in the application delegate. The current code inside the didFinishLaunchingWithOptions method is:
func setBackground() {
let width = self.window!.frame.size.width// * UIScreen.mainScreen().nativeScale
let height = self.window!.frame.size.height// * UIScreen.mainScreen().nativeScale
let size = max(width, height)
UIGraphicsBeginImageContext(CGSize(width: width, height: height))
UIImage(named: "background.jpg")?.drawInRect(CGRectMake(-(size - width) / 2, -(size - height) / 2, size, size))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.window?.backgroundColor = UIColor(patternImage: image!)
}
This works like it supposes, but with one issue. The image looks like it is scaled down and scaled back to the scale of the screen. So on an iPhone 6 Plus it doesn't look sharp. This might be expected behaviour since the window size is not the full resolution. But when I multiply it with the native scale, only 1/9th the image (upper left) is shown. Is there a way (besides providing separate resolutions for this image) to show it without scaling first?
A second issue, for if anyone know the solution for this. The image is shown behind a UINavigationController where every view inside the UIViewController has a transparent background. This works and shows the background, but one problem, the background turns a little darker when navigation to another view. Probably because the animation. Is there a way to hide this animation? Inside the UIViewControllers (there are only two yet, but this will be inside a class which will be overridden) is the following code:
override func viewWillAppear(animated: Bool) {
self.view.alpha = 1.0
super.viewWillAppear(animated)
}
override func viewWillDisappear(animated: Bool) {
UIView.animateWithDuration(0.25, animations: {
self.view.alpha = 0.0
})
super.viewWillDisappear(animated)
}
Even without this code, it still turns darker before it goes light again. It looks like the view still as a dark opaque background with a very low alpha and when presenting the second screen, it lays both backgrounds on top of each other until the second screen is completely presented and the first one will be removed.
Thanks!

TabBar icon becoming a rectangle when its state is selected

I use two icons for different states tabBarItem.
My problem is that when tabbar is selected one icon to become a rectangle.
I did the other icons, and they appear well. I was looking for any information not found on this topic. How can I fix it?
My code
override func viewDidLoad() {
super.viewDidLoad()
let triviaMainTableViewController = StoryboardManager.triviaStoryboard.instantiateViewControllerWithIdentifier("TriviaMainTableViewController") as! TriviaMainTableViewController
viewControllers = [triviaMainTableViewController]
tabBarItem.image = UIImage(named: "TriviaTabBarDefault")?.imageWithRenderingMode(.AlwaysOriginal)
tabBarItem.selectedImage = UIImage(named: "TriviaTabBarSelected")
tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -3)
navigationBar.barTintColor = ColorManager.greenColor
}
You need to make sure you have put your icon on a transparent background in order for the selection highlight to work correctly. If the background color of the image isn't transparent it may look fine when it is not selected, but not when it is selected.

Adding gradient to scrolling UITableView

I was able to add a gradient my UITableView, but I have the issue of when I have to scroll through my cells, the gradient background scrolls along also. I want the background to stay consistent as I scroll up or down. How can I achieve this? Do I have to create a custom UITableView in order to do this?
The pictures below show what it currently looks like.
Here is my code for adding the gradient to the UITableView:
func addGradientToBackground(){
var gradient : CAGradientLayer = CAGradientLayer()
gradient.frame = self.tableView.bounds
gradient.colors = [UIColor.blueColor().CGColor, UIColor.redColor().CGColor]
self.tableView.layer.insertSublayer(gradient, atIndex: 0)
}
Messing around will setting the gradient doesn't work either, like setting:
self.view.layer.insertSublayer(gradient, atIndex: 0)
or changing the bounds:
gradient.frame = self.tableView.frame
Also, in cellForRowAtIndexPath I set the UITableViewCells background color to clear:
cell.backgroundColor = UIColor.clearColor()
I can't add images, but here is the link if you wish to see them: http://imgur.com/Vtka1tO,6faLMkr#0
The gradient needs to be behind the table view if you don't want it to scroll. If you're using a UITableViewController, the only thing behind is the window, so you could give it the gradient, and make the cells and the table view have a clear background color. If you're using a UIViewController with a table view as a subview, then you could give the controller's main view a gradient background color.
Have you thought to add a background image as follows?
var imageView = UIImageView(frame: CGRectMake(10, 10, cell.frame.width - 10, cell.frame.height - 10))
let image = UIImage(named: ImageNames[indexPath.row])
imageView.image = image
cell.backgroundView = UIView()
cell.backgroundView.addSubview(imageView)

Getting the UIKeyboard background color programmatically

Is there any way to get the UIKeyboard background color? I am putting an accessory view on top of my UIKeyboard and is trying to match its color with the keyboard background color. But, it seems, different types of keyboard has different background colors. Please see below screenshots for default and email keyboard.
Is there any way, we can find out the background color of the keyboard programmatically so that the color of the accessoryView could be changed.
Swift 5
You can use an input view and it matches the keyboard style.
let textView = UITextView()
let frame = CGRect(x: 0, y: 0, width: 0, height: 40)
textView.inputAccessoryView = UIInputView(frame: frame, inputViewStyle: .keyboard)
You could do something like this:
UIKeyboardAppearance currentAppearance = yourTextView.keyboardAppearance;
if (currentAppearance == UIKeyboardAppearanceDark) {
// dark
}
else if (currentAppearance == UIKeyboardAppearanceDefault) {
// default
}
else if (currentAppearance == UIKeyboardAppearanceLight) {
// light
}
I suggest you to get your keyboard color's RGB from DigitalColor Meter as we use this tool in Mac or you can get it by any other tool.
And then simply assign these RGB value to your accessory view and match its color with your keyboard.
The RGB value of your keyboard is seems to me is like (63,63,63) and you can use this:
[UIColor colorWithRed:63/255.0 green:63/255.0 blue:63/255.0 alpha:1]
Hopefully it will helps you.
If you're developing a custom keyboard and trying to match the background of your keyboard's view, use this in your keyboard's view controller where you set up the view:
keyboardView.backgroundColor = view.backgroundColor
My viewDidLoad() in my keyboard's view controller looks like:
var keyboardView: UIView!
let keyboardNib = UINib(nibName: "KeyboardView", bundle: nil)
keyboardView = keyboardNib.instantiate(withOwner: self, options: nil)
[0] as? UIView
keyboardView.frame.size = view.frame.size
keyboardView.backgroundColor = view.backgroundColor
view.addSubview(keyboardView)
For the dark background use
mytextfield.keyboardAppearance = UIKeyboardAppearanceAlert;

Resources