How can I have a background not cover every button, label? - ios

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

Related

Correct placement of UIImageView in UIBarButtonItem

When attempting to place a UIImageView(UIImage) into a UIBarButtonItem on a UINavigationBar, the image gets placed in the middle of the bar and also has wide fields covering the entire bar. So, doesn't look like a small button on the left.
I've tried various tricks with frame resizing, contentMode settings.
The below code is from my View Controller, which is part of the Navigation Controller stack. Added this image into Assets:
http://pluspng.com/img-png/png-hd-bike-ktm-bike-png-500.png
for testing, named it bike.png and used it in UIImage below.
override func viewDidLoad() {
super.viewDidLoad()
let image = UIImage(named: "bike")
let imageView = UIImageView(image: image)
imageView.backgroundColor = .blue //for debugging
imageView.contentMode = .scaleAspectFit
navigationItem.leftBarButtonItem = UIBarButtonItem(customView: imageView)
}
The expected result would be to have the image of the motorbike in the left, rather than in the middle. Also, no empty fields to the left and to the right (highlighted in blue for debugging) of the image.
EXPECTED (drew up in Paintbrush):
REALITY:
Ended up resolving as:
imageView.widthAnchor.constraint(equalToConstant: (navigationController?.navigationBar.frame.height)!).isActive = true

Xcode 9 Ios11 -> UITableViewController won't show anything

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

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

How to use an image logo on the navigation bar instead of title

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()
}

Navigation Controller image instead of text

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)

Resources