I've got a grey custom image that I'm using as my background for a UITableView. Image is PNG with the appropriate alpha settings, has rounded corners, and loads fine. Despite using the following, however, I still see a black "background" where the corners become transparent.
tableView.backgroundView = UIImageView(image: UIImage(named: "someImage"))
tableView.backgroundColor = UIColor.clear
I also set my TableView background to bright colors to see if that's where the issue was, however still black on the background.
How do I get this area to be completely transparent so it looks as if my round corners are the end of the TableView?
Thanks!
Try the snippet. Hope this works
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "bg")
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
Related
I can't figure out why this happens, but when I add a imageview behind my tableview and navigation bar, my navigation bar acts strange:
The navigation bar gets stuck in a static position and dosenĀ“t move while scrolling as it did before
The activity indicator from pull to refresh gets placed between the navigation bar button and the top of the tableview. Instead of staying on top of the navigation bars title as before.
Left picture is the bug without the imageview in the background.
Right picture show how it works perfectly without the imageview.
My code to get the clear navigation bar (Though I don't think this introduces any problem as it works fine when the imageview in the background isn't there):
self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
self.navigationController?.navigationBar.shadowImage = UIImage()
self.navigationController?.navigationBar.isTranslucent = true self.navigationController?.view.backgroundColor = UIColor.clear
Found a solution.
Set the image as the backgroundview of the tableview and set the tableview to be fullscreen.
let image = UIImage(named: "myphoto")
let iv = UIImageView(image: image)
iv.contentMode = .scaleAspectFill
iv.layer.frame = CGRect(x: 0, y: 0, width: (self.tableView.superview?.frame.size.width)!, height: (self.tableView.superview?.frame.size.height)!)
let tableViewBackgroundView = UIView()
tableViewBackgroundView.addSubview(iv)
self.tableView.backgroundView = tableViewBackgroundView
I have created extension for UITextView, where I set image to the textview using below code.
let backgroundImage = UIImageView(frame: ....)
backgroundImage.image = UIImage(named: "textarea.png")
self.addSubview(backgroundImage)
self.sendSubview(toBack: backgroundImage)
At start it looks fine as showing below image (left side), however as I add multi-line text, image also starts scrolling (right side), which is incorrect.
Any idea how to make image fixed as it is?
Below is what I did
let backgroundImage = UIImageView(frame: ....)
backgroundImage.image = UIImage(named: "textarea.png")
mainView.insertSubview(backgroundImage, belowSubview: self)
mainView is nothing but the main view (self.view) of view controller.
This is like we are not going to add backgroundImage in the subview of textview, instead we will be adding in the mainview (self.view).
A simple solution:
A container UIView contains background UIImageView and TextView(with clear background color).
Im having a difficult time trying to change the navigationBar.barTintColor to white. Strangely, all other colors work, but not white!
self.navigationController!.navigationBar.barTintColor = UIColor.whiteColor()
Above line doesnt work just for white.
Even tried with a background image. Still the same. Any other color works but not white!! White is always replaced by light grey...
Please advice where I am going wrong...
Thanks.
Try this Code:
In your viewDidLoad:
title = "Some Title"
UIApplication.shared.statusBarStyle = .default
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
navigationController?.navigationBar.shadowImage = UIImage()
navigationController?.navigationBar.isTranslucent = true
//Line under NavigationBar
let barView = UIView(frame: CGRect(x:0, y:(UINavigationController().navigationBar.frame.height + UIApplication.shared.statusBarFrame.height), width:view.frame.width, height:0.6))
barView.backgroundColor=UIColor.red // set any colour you want..
navigationController?.navigationBar.addSubview(barView)
//Title Colour
navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.red]
Note:
Above code works on any background colour....
If you want the navigationBar to be green....set your view background colour to green...
I tried using the code below for setting the background for cells. It works fine but it obscures the disclosure indicator. How do I pull the disclosure indicator forward so that it is visible?
cell.backgroundColor = UIColor.clearColor()
let imageView = UIImageView(frame: CGRectMake(0, 0, cell.frame.width, cell.frame.height))
let image = UIImage(named: "sand background 1.png")
imageView.image = image
imageView.alpha = 0.5
cell.addSubview(imageView)
cell.sendSubviewToBack(imageView)
When I do this, the disclosure shows up fine. I'd personally do insertSubview(atIndex: 0) rather than addSubview and sendSubviewToBack if I wanted to manually add the background image view in the view hierarchy manually. Or, I would just set the cell's backgroundView (and selectedBackgroundView). But all of these approaches have the disclosure indicator visible (assuming that the disclosure indicator was turned on) for me. I'd run the app and then use the view debugger and carefully examine the view hierarchy and confirm that the disclosure indicator is really there:
You can then select the debug navigator in the left panel and choose the "View UI Hierarchy" from the button in the top right corner of the debug navigator:
This way you can confirm that the disclosure button is really there, or whether the disclosure indicator is simply not visible (e.g. its color might be too close to your sand-colored background to stand out).
If the problem is that the color of the accessory view is too similar to the background color, you can change the accessory view (see https://stackoverflow.com/a/27971576/1271826). For some accessory types (namely the details or checkmarks), you can change the color simply by changing the tintColor of the tableview. If you want to change the color of the disclosure indicator, you may have to replace the accessory view with a disclosure image that you created yourself, e.g.:
cell.accessoryView = UIImageView(image: accessoryImage)
You can either build this accessory image and add it as an asset to your project, or you can define an accessory image that you build dynamically. For example, here is a yellow disclosure accessory image:
lazy var accessoryImage: UIImage = {
let rect = CGRect(x: 0, y: 0, width: 10, height: 43.5)
let insetRect = CGRectInset(rect, 2, 15)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0)
UIColor(red: 1, green: 1, blue: 0, alpha: 1).setStroke()
let path = UIBezierPath()
path.moveToPoint(CGPoint(x: insetRect.origin.x, y: insetRect.origin.y))
path.addLineToPoint(CGPoint(x: insetRect.origin.x + insetRect.size.width, y: insetRect.size.height / 2.0 + insetRect.origin.y))
path.addLineToPoint(CGPoint(x: insetRect.origin.x, y: insetRect.size.height + insetRect.origin.y))
path.lineWidth = 2
path.stroke()
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}()
That yields:
I want to set background image for a label in my project but i dont know for how to set, please say it is possible or not?
This should do the trick:
yourLabel.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage")!)
The UILabel control does not have background image or image property.
You can set image to UILabel using backgroundColor property as shown below :
cell.lblNumber!.text = ""
cell.lblNumber!.backgroundColor = UIColor(patternImage: UIImage(named: "OK")!)
Set the text to empty since it will display image and text on top of it.