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)
Related
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
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
I have a logo that appear on top of all my UIViewController(controlled by NavigationController).
When I get the transition between UIViewController, the logo also swipe.
What I want: I want to logo(60 pixel height) to always stay. And the swipe transition to appear below the logo.
I've tried this in view delegate, but the logo also swipe :(
let logo = UIImage(named: "logo.png")
let imageView =UIImageView(image:logo)
self.navigationItem.titleView = imageView
Issue: You have the logo swiping when transiting to next VC.
Desired Result: You want to have the logo show up on each VC without the swipe animation. But still have the Swipe animation running for everything below the logo.
What I would do is make a UIScrollView and keep the logo as just a UIImageView.
If you have github repo of this app I can take a look for you.
Subclass the UINavigationController and in the viewDidLoad, configure the logo image view and add it to the navigationBar as a subview. Also, since we're adding it to the navigation bar which is only 44px in height, so a logo with a height of 60px would overflow the bounds.
class NavigationController: UINavigationController {
override func viewDidLoad() {
super.viewDidLoad()
let logo = UIImage(named: "logo.png")!
let imageView = UIImageView(image: logo)
let width = logo.size.width * 60 / logo.size.height
imageView.contentMode = .scaleAspectFit
imageView.frame.size = CGSize(width: width, height: 60)
imageView.center = navigationBar.convert(navigationBar.center, to: navigationBar)
navigationBar.addSubview(imageView)
}
}
For an app I am working on, the profile page has a horizontal scroll across the top, in which there are 3 views, each spanning the width of the device. On one view is the profile picture, on the other are details and the other are social links.
The problem I am getting is when I create a UIImageView called profileImage, and then assign the users profile image to this view, it all worked fine before I added this scroll view.
Then I created this scroll view, and profileImage sits in firstView, the first view of the scroll. However the image never appears, this is not just a problem with the image as I have also attempted adding default images in the assests.
var profileImage: UIImageView!
below is the code block for creating the firstView of the slide and adding the image to the image view.
let firstView = UIView(frame: CGRectMake(scrollView.frame.origin.x, scrollView.frame.origin.y, scrollView.frame.size.width, scrollView.frame.size.height))
firstView.backgroundColor = Colours.blue
scrollView.addSubview(firstView)
var img = UIImage(named: "default_image")
profileImage = UIImageView(image: img!)
profileImage.contentMode = UIViewContentMode.ScaleToFill
if let imageUrl = user.profilePictureUrl {
profileImage.hnk_setImageFromURL(imageUrl)
println("THIS IS IMAGE URL: \(imageUrl)")
}
profileImage = UIImageView(frame: CGRectMake((firstView.bounds.size.width - profileImage.frame.size.width) / 2.0, 10, 80, 80))
firstView.addSubview(profileImage)
firstView.bringSubviewToFront(profileImage)
The imageUrl appears, I know I am missing something really easy. Also I want to note that the UIImageView and the UIScrollView are not IBOutlet's but created programmatically.
You have instantiated UIImageView object twice with same name "profileImage"; so your first profileImage is being overrided by the next profileImage. Comment in the last third line and set the frame like:
profileImage.frame = CGRectMake(x,x,x,x)
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