set uitableview background view - ios

I want to set the background view for my tableview,
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let backgroundView = BackgroundGradient(frame: self.view.frame)
self.tableView.backgroundView = backgroundView
}
however the background remains white (it should be a blueish gradient). I have tried to set it in view did load, same result, I changed to background color to clear, and still nothing. what am I missing?

Your UITableViewCell could be obscuring the backgroundView of your table. Try setting your cell's background color to clear.
Quote from Apple Docs (https://developer.apple.com/reference/uikit/uitableview/1614986-backgroundview)
You must set this property to nil to set the background color of the table view.

Try using:
self.tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundImage.png"))
See if this works then there must be some issue with backgroundView

i found the problem... i had forgotten that UIColor(red:green:blue:alpha:) takes CGFloats between 0 and 1 instead of 0-255 as usualy, fixed it and it works fine now... Thanks all

Related

Why doesn't setting backgroundColor have a visible effect on my custom UIView?

When I set the backgroundColor property of my Xib-instantiated subclass of UIView, the view's background color doesn't change but rather stays the color set in the Xib.
Reproducing the issue
I have created a subclass of UIView, let's call it MyView, along with a Xib from which I instantiate it. Using Interface Builder, I have set the background color of the view in the Xib to blue.
I have added an instance of MyView as a subview of my app's main view. It appears blue, as expected.
I then added an outlet, myView, referencing that instance of MyView, and a button whose action attempts to change the view's background color like so:
#IBOutlet weak var debugView: MyView!
#IBAction func debug(_ sender: Any) {
myView.backgroundColor = UIColor.green
}
I expected the background color of myView to change to green; instead, it stays blue like before.
Observations
Strangely, if I check the background color of the view in the debugger, it seems that the property has been set to green, as expected, even though the change isn't visible:
(lldb) p debugView.backgroundColor == UIColor.blue
(Bool) $R1 = false
(lldb) p debugView.backgroundColor == UIColor.green
(Bool) $R2 = true
I have also tried forcing the view to redraw using setNeedsDisplay(), to no effect.
Why doesn't my custom view's appearance reflect its backgroundColor property?
You probably added a view from nib as subview to your customview in the swift file that is the reason on changing the backgroundcolor you would not see the changes. you need to change the background color of the subview of your debugview to see the color change.
For example:-
self.debugView.subviews.first?.backgroundColor =
self.debugView.subviews.first?.backgroundColor == UIColor.blue
? UIColor.black : UIColor.blue

Fixed background in a TableView?

Fixed Background image in a TableView ?
Hey guys !
My first question as a Swift nOOb !
I'm trying to set up a fixed image as a background for my Table View. So far, the best option has been to include this in my ViewDidLoad :
let uluru = UIImage(named: "Uluru")
self.view.backgroundColor = UIColor(patternImage: uluru!)
Not so great, right?
Especially because when you're scrolling, the image is tiled. Meaning, it's repeating itself. Does anyone has a solution via the IB or directly into the code to make it fixed ? Something like in CSS ?
I also tried the superview way :
override func viewDidAppear(animated: Bool) {
let uluru = UIImage(named: "Uluru")
let uluruView = UIImageView(image: uluru)
self.view.superview!.insertSubview(uluruView, belowSubview:self.view)
}
But no success at all!
And last but not least :
override func viewDidLoad() {
super.viewDidLoad()
let backgroundImage = UIImageView(frame: UIScreen.mainScreen().bounds)
backgroundImage.image = UIImage(named: "Uluru")
self.view.insertSubview(backgroundImage, atIndex: 0)
}
Thank you all!
Do not use the backgroundColor property for this, and do not add any subviews. The table view is all ready for you to do what you want to do. Like this:
Create an image view (UIImageView) whose image is the desired image.
Make that image view the table view's background view (its backgroundView property).

How to set background image for tableView in PFQueryTableViewController

This code in viewDidLoad works for adding a background image to a tableView in a generic tableViewController, however using the Parse SDK, it does not add a background image in a PFQueryTableViewController. What am I doing wrong in the PFQTVC?
peopleTableView.backgroundView = UIImageView(image: UIImage(named: "SFStreetcar"))
Turns out simply placing the code inside viewWillLayoutSubViews did the trick:
override func viewWillLayoutSubviews() {
tableView.backgroundView = UIImageView(image: UIImage(named: "SFStreetcar"))
}
If that doesn't work though #user2792129 's solution is worth exploring as well.

Separator lines appearing around UITableViewCell on select/highlight in iOS8

I have a very simple UITableView where I have set the separator inset style to be none:
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
Running the app I get what I expect with no lines between the cells. However, when I select one of the table view cells, white lines appear around it. Why is that and can I change the appearance of it? Screenshots below:
The lines shown by arrows appear on highlight. The cell colour change is intentional. Any help in the right direction would be much appreciated. I am using iOS 8.1 as target with XCode 6.1.1
I have set the background colour as follows.
In viewDidLoad:
tableView.backgroundColor = UIColor.blackColor()
tableView.alpha = 0.8
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
In cellForRowAtIndexPath:
thisCell.contentView.backgroundColor = UIColor.blackColor()
thisCell.contentView.alpha = 0.85
I'm assuming that you are overriding setSelected and setHighlighted?
self.selectedBackgroundView.hidden = true
works for me.
It took me a while to find a solution to it as well. And this is the view hierarchy that I found for the cell:
0: class: 'UITableViewCellSelectedBackground'
0: class: 'UITableViewCellContentView'
0: class: '_UITableViewCellSeparatorView'
The UITableViewCellSelectedBackground is setting the background. The background is then overlayed with the ContentView.
Could you please say how you made such background in cells? In my opinion it's not selection lines, it just whole grey background, that appear under you cell black backgrounded view while section or highlighting. You could use debug view heresy or just override this two functions:
func setSelected(selected: Bool, animated: Bool)
func setHighlighted(highlighted: Bool, animated: Bool)

How to remove the outer border of UITextField presented in UIAlertActionController

I am adding multiple UITextFields to my alert controller, and I don't want that ugly black box around them. I know it's not the border property of the textField, because I have tried setting that and that influences the actual textField, not the box.
I have tried
textField.superview?.backgroundColor = UIColor.redColor()
textField.superview?.layer.borderColor = UIColor.redColor().CGColor
Setting the background Color works as expected, filling in the space between the textfield and the "black border" but setting the borderColor or borderWidth on superview.layer do nothing. Any ideas?
I ended up subclassing UIAlertController.
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
for field in textFields! as [UITextField] {
field.superview?.superview?.layer.borderWidth = 2
field.superview?.superview?.layer.borderColor = UIColor.whiteColor().CGColor
}
}

Resources