I have a big problem and I searched already but didn't find anything.
All worked and then I put this code in my viewDidLoad() of the class UITableViewController.
let imageView = UIImageView(frame: self.view.frame)
let image = UIImage(named: "sample")!
imageView.image = image
self.tableView.backgroundView = imageView
If I run it, it just shows the background and some lines from the cells but no text:
Like this
Related
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))
I have tried a couple different codes but couldn't make the logo show
import UIKit
class HomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image: logo)
self.navigationItem.titleView = imageView
navigationItem.titleView?.sizeToFit()
}
}
I even tried to include IB in the class itself didn't work either, it doesn't seem to work that way
#IBOutlet weak var navBar: UINavigationItem!
PS. My logo is a 200x40px png and its named logo.png in the assets.
My storyboard
http://i68.tinypic.com/b68t8o.png
Any help is appreciated
Edit: I solved my problem by putting an image view there instead of this whole navigation item story. Thanks for your suggestions though.
try this
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image: logo)
imageView.contentMode = .ScaleAspectFit // set imageview's content mode
self.navigationItem.titleView = imageView
Set your imageview's content mode like,
imageView.contentMode = .ScaleAspectFit
This code looks good, i think there is error in name of UIImage, did you debug and check that let logo is not nil? try to remove ".jpg"
Use extension:
extension UIViewController {
func setNavigationBarLogo() {
let logo = UIImage(named: "YourLogo.png")
let imageView = UIImageView(image: logo)
imageView.contentMode = .scaleAspectFit
self.navigationItem.titleView = imageView
}
}
Usage in UIViewController:
override func viewDidLoad() {
super.viewDidLoad()
self.setNavigationBarLogo()
}
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).
I am trying to put a logo (png) file into my navigation controller. I found one solution that "worked" but it didn't scale down the image to fit, thus is was massive
This is the code im using thus far, but it makes the image huge
let logoImage:UIImage = UIImage(named: "logo.png")!
self.navigationItem.titleView = UIImageView(image: logoImage)
Try this:
let logo = UIImage(named: "logo.png")
let imageView = UIImageView(image:logo)
imageView.contentMode = UIViewContentMode.ScaleAspectFit //you need to set this.
self.navigationItem.titleView = imageView
And if you want to give height and width then add this:
imageView.frame.size.width = 200
imageView.frame.size.height = 45
You can do it in 2 ways: programmatically or via XCode:
An easy way is via XCode. For it, drag view from the object library and drag it to your navigation bar. Later drag into your view in navigation bar UIImageView from object library. That's all.
Or you can do all of this programmatically.
var myView = UIView()
var imageView = UIImageView()
imageView.image = UIImage(named: "your_image_here")
myView.addSubview(imageView)
navigationController?.navigationBar.addSubview(myView)
Hi I am making an app with a background that leaves a big blank white space at the top of the screen when I use the first code shown below and as soon as I add self.view.addSubview(image view) at the end of this code like the second piece of code it covers all my labels and buttons, how can I have it so that it takes up the whole screen like in the second part but doesn't cover up the buttons like in the first piece of code?
thanks !
first code
override func viewDidLoad() {
super.viewDidLoad()
let yourImage = UIImage(named: "fond320x480.png")
let imageview = UIImageView(image: yourImage)
self.view.addSubview(imageview)
self.view.sendSubviewToBack(imageview)
imageview.contentMode = UIViewContentMode.ScaleToFill
imageview.contentMode = UIViewContentMode.ScaleAspectFill
imageview.frame = self.view.bounds
second code:
override func viewDidLoad() {
super.viewDidLoad()
let yourImage = UIImage(named: "fond320x480.png")
let imageview = UIImageView(image: yourImage)
self.view.addSubview(imageview)
self.view.sendSubviewToBack(imageview)
imageview.contentMode = UIViewContentMode.ScaleToFill
imageview.contentMode = UIViewContentMode.ScaleAspectFill
imageview.frame = self.view.bounds
self.view.addSubview(imageview)
try to send it to the back using this method
insertSubview:yourSubView atIndex:0
and give it the index by zero.
or use this one
-insertSubview:aboveSubview:
the other solution is to add all your view in one view and then add the image and use method -belowSubView- to add the image under the view that has all your views
hope this answer can help good luck