Cant get rid of grey colour behind TabBar in Swift - swift

I put a background image to my view, so the full view should have it.
let background = UIImage(named: "background")
var imageView : UIImageView!
imageView = UIImageView(frame: view.bounds)
imageView.contentMode = UIView.ContentMode.scaleAspectFill
imageView.clipsToBounds = true
imageView.image = background
imageView.center = view.center
view.addSubview(imageView)
self.view.sendSubviewToBack(imageView)
Also I have a barTintColor for my tabBar:
tabBar.barTintColor = UIColor.blue
The problem is when my TabBar is transparent for some percent, I can see behind the grey color(probably the default color) instead of my view's background image...
Debug view hierarchy:

Try the following
UITabBar.appearance().isTranslucent = false

Related

Is there a way to make a completely transparent UIButton that works?

I've a custom UINavigationItem title view. It has a label pushed to the top of it, which I've enabled to respond to .touchDown and send an action.
However, the taps don't register near the top of the label, presumably because the active region is clipped. So I configured another invisible view (not in navigation item), and set it up as a control, and positioned it above that navigation title view label.
However, it doesn't work unless I set the 'invisible' view's alpha to at least 0.02, because Apple seems to intentionally disable action for a view with an alpha less than that. Unfortunately, against a black screen in dark mode, the 'invisible' hitpad view shows up as a slightly grey rectangle, which is not a good aesthetic.
I guess I could go to some lengths to try to make the button background color match the screen background at that location, but it seems a bit tacky.
What alternatives might I have?
You can simply create a blank png image, and add it in top of your title view. make sure to set the imageView and the title view isUserInteractionEnabled properties to true:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
let imageView = UIImageView()
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(
target: self,
action: #selector(tap)
)
imageView.addGestureRecognizer(tap)
imageView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
imageView.image = UIImage(named: "blank")
let titleLabel = UILabel()
titleLabel.text = "Transparent Button"
titleLabel.autoresizingMask = [.flexibleHeight, .flexibleWidth]
titleLabel.addSubview(imageView)
navigationItem.titleView = titleLabel
navigationItem.titleView?.isUserInteractionEnabled = true
}
#objc func tap(_ gesture: UITapGestureRecognizer) {
print(#function)
}
}
Sample project
You can also just add your gesture recognizer directly to your titleView. No need for the transparent image at all unless you need to control the area of the gesture:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
let tap = UITapGestureRecognizer(
target: self,
action: #selector(tap)
)
let titleLabel = UILabel()
titleLabel.text = "Transparent Button"
titleLabel.autoresizingMask = [.flexibleHeight, .flexibleWidth]
titleLabel.addGestureRecognizer(tap)
navigationItem.titleView = titleLabel
navigationItem.titleView?.isUserInteractionEnabled = true
}
#objc func tap(_ gesture: UITapGestureRecognizer) {
print(#function)
}
}
This is an adaptation of the #LeoDabus' Accepted answer that works. However it was utterly informed by his explanation and example. The only meaningful change I made to Leo's example was to create a real empty image programmatically, and drop the label generation. Without a real empty UIImage(), the only way to make taps on the region work that I found is to set the image view's background color to non-clear.
func emptyImage(with size: CGSize) -> UIImage?
{
UIGraphicsBeginImageContext(size)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
func configureButtons() {
let imageView = UIImageView(image: emptyImage(with: CGSize(width: view.frame.size.width - 250, height: 44)))
imageView.frame = CGRect(x: 140, y: self.view.safeAreaInsets.top + 50,
width: view.frame.size.width - 250, height: 44)
imageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(actionEnableTitleEditing))
imageView.addGestureRecognizer(tap)
imageView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
view.addSubview(imageView)
}

UIImage not resizing according to UIImageView size width and height

I am a Swift newbie and I am trying to add an UIImageView on the right of UITextField using Storyboard and #IBInspectable annotation.
However, when I define my UIImageView with specific size and add the UIImage, the UIImage is not resizing according to UIImageView size.
Here's my code:
#IBInspectable var rightImage: UIImage? {
didSet {
guard let image = rightImage else {
return
}
rightViewMode = .always
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 20, height: 20))
imageView.image = image
imageView.contentMode = .scaleAspectFit
rightView = imageView
and here's the result on the screen:
Big big icon
If you can tell me what is wrong with my code, do not hesitate.
Thanks.
I use constraints based on Matt’s suggestion in the comment, and it's working now:
let constraints = [
imageView.heightAnchor.constraint(equalToConstant: 20),
imageView.widthAnchor.constraint(equalToConstant: 20)
]
NSLayoutConstraint.activate(constraints)

swift navigation bar title image position

I created a navigation bar title image, it is working well.
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 5, height: 10))
imageView.contentMode = .scaleAspectFit
let image = UIImage(named: imageTitle)
imageView.image = image
navigationItem.titleView = imageView
but i added an item button at the right side of navigation bar, the title image move to left a bit. How to make the image title in the center of the screen?

how to create uiview with transparent background?

I saw this image in patterns.com. I try to create a modal with transparent background but it makes other contents inside uiviewcontroller to become transparent. any ideas?
As below image
Blur Effect : Swift 3
//add blur effect
let blurEffect = UIBlurEffect(style: UIBlurEffectStyle.light)
let blurEffectView = UIVisualEffectView(effect: blurEffect)
blurEffectView.frame = self.view.bounds
blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.view.addSubview(blurEffectView)
and this to delete it :
blurEffectView.removeFromSuperview()

Resize My TableView Background

I have changed my TableView background by using this code
tableView.backgroundView = UIImageView(image: UIImage(named: "wall"))
I would like to resize the background programmatically
thank you
Have you tried changing the backgroundView's frame?
var tableView = UITableView()
tableView.backgroundView?.frame = CGRect(x: yourX, y: yourY, width: yourWidth, height: yourHeight)
EDIT
For altering the content mode for the image, you can do something like this:
var imageView = UIImageView(image: UIImage(named: "image"))
imageView.contentMode = .scaleAspectFill
or
imageView.contentMode = .scaleToFill
or
imageView.contentMode = .scaleAspectFit
You can see which one works best for you. There are also a few more, so you can choose the best one.