How to set background image for tableView in PFQueryTableViewController - ios

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.

Related

How to make page like Appstore in iOS 11

I'm working on my own app in which I want to build a page almost like Appstore page. I googled and found solution for Large text but not able to find for image on navigation bar as shown in attached screenshots with this.
Is anybody done this kinda work, need help and suggestions ?
image on navigation bar: you can use navigationItem.titleView add a UIImageView.
Try this
import UIKit
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let logo = UIImage(named: "logo")
let imageView = UIImageView(image:logo)
imageView.contentMode = .ScaleAspectFit
self.navigationItem.titleView = imageView
}
}
You can do this with one line:
self.navigationItem.titleView = UIImageView.init(image: UIImage.init(named: yourImageName))

set uitableview background view

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

Swift / cornerRadius and masksToBounds lets UIImageView disappear

I have a UITableViewController with a UIImageView. The content is loaded with the following simple code.
#IBOutlet weak var avatarImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
updateUI()
}
func updateUI() {
backendless.userService.currentUser.retrieveProperties()
avatarImageView.layer.cornerRadius = avatarImageView.frame.size.width/2
avatarImageView.layer.masksToBounds = true
let imgURL = NSURL(string: (backendless.userService.currentUser.getProperty("Avatar") as? String)!)
avatarImageView.sd_setImageWithURL(imgURL)
tableView.reloadData()
}
The ImageView does not appear.
But if I delete the following code, the UIImageView will be set and displayed correctly.
avatarImageView.layer.cornerRadius = avatarImageView.frame.size.width/2
avatarImageView.layer.masksToBounds = true
What am I missing? Help is very appreciated.:
PS:
Xcode 8.0
Swift 2.3
Contrains for ImageView are set to 120*120
sd_setImageWithURL = pod 'SDWebImage'
Try this:
override func viewDidLoad() {
super.viewDidLoad()
self.view.layoutIfNeeded() // <- This will update your initial view frames
updateUI()
}
What is happening is that in Xcode 8.0, by the time you hit viewDidLoad, the frame of all subviews of a controller is still x: 0.0, y: 0.0, w: 1000.0, h: 1000.0, so in effect you're making the corner radius of your avatar image view 500.0pt. When you call layout if needed, the correct frames will be calculated, and give you the value you're expecting.
One obvious problem is that you are calling updateUI too soon. In viewDidLoad, there is no UI. The view has loaded but that is all; it is not in the interface and no layout has yet taken place. Your code depends upon avatarImageView.frame, but we do not yet know what that is. You have constraints on the image view, but they have not yet taken effect! Thus you should postpone this call until after the interface is present, e.g. viewDidAppear or at least viewDidLayoutSubviews. But of course then you probably only want to call it once (typed in browser, not tested, but it will be something like this):
var didConfigureUI = false
func viewDidLayoutSubviews() {
if !didConfigureUI {
didConfigureUI = true
updateUI()
}
}
I think your problem with your controller. You have added the image view to a UITableViewController. That means constraint set to image view will not work if you use the frame in view did load. You need to change frame of the UIIMageView before use it or you need to use UIViewController. use the below code before using the frame of image view.
avatarImageView.frame.size.width = 120.0
avatarImageView.frame.size.height = 120.0
I think this will solve the problem. But you should use UIViewController if there is no special purpose of UITableViewController.
“xy.view.layoutIfNeeded()”
needs to be called everytime i’m calling a property before the view has finished loading
for example imageView.view.layoutIfNeeded() to call for the height and widths values

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).

Why can't I change the view's frame size in Swift?

So this is the weirdest thing I came across ever.
I add a simple UIView on top of an UIView on an UIViewController. I change the size to 200 x 200 and then I want to change the UIView's size by placing this line of code inside the viewDidLoad method:
self.gameBackgroundView.frame.size.height = 10
which seems perfectly normal in Swift. Though, nothing happens. I can change the color, corner radius etc...
EDIT:
I have also deleted all constraints from that view and it still does not work.
Ok, So I found a solution here on stack ..
Change frame programmatically with auto layout
The correct answer, that works is, that:
I need to create an IBOutlet of my heightConstraint
Change the value of that constraint accordingly
Thank you all!
It is because of Auto Layout, however, you could do it after Auto Layout is done its work or change constraints of it. Set it your frame in your viewDidAppear() instead. It should work.
I hope this helps you.
Try this code, it will work on any device
let BackImage = UIImage(named: "backGrndImg#2x.png")!
var imageView: UIImageView!
override func loadView() {
super.loadView()
self.imageView = UIImageView(frame: CGRectZero)
self.imageView.contentMode = .ScaleAspectFill
self.imageView.image = BackImage
self.view.insertSubview(imageView, atIndex: 0)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.imageView.frame = self.view.bounds
}

Resources