I'm trying to add a title image to my nav bar in the entirety of my app. The below line of code in my App Delegate works, but the image is tiled to fit... Is there a way I can set the dimensions and position?
UINavigationBar.appearance().setBackgroundImage(UIImage(named: "tmp logo.png"), forBarMetrics: .Default)
You can set the size of image, by setting the size of UIImageView.
var imageNew = UIImageView(frame: CGRectMake(10, 10, 100, 100));// define
the size of the view here.
var image = UIImage(named: "myImage.png");
imageNew.image = image;
self.view.addSubview(imageNew);
Pl. refer to the below stackoverflow response, which provides the solution
How do set a width and height of an Image in Swift
Related
I have an UIImageView which is as big as the whole view. When I insert an image, I would like for the image view to shrink itself in order for it to be as big as the image I insert itself. I cannot find a way to do it.
There are a number of ways you can approach this assuming you have a UIImageView declared like so:
#IBOutlet var dynamicImageView: UIImageView!
When changing your image, set the imageView size to that of the UIImage e.g:
func changeImage(){
if let image = UIImage(named: "Octocat"){
dynamicImageView.image = image
dynamicImageView.frame.size = image.size
print("Size Of ImageView = \(dynamicImageView.frame.size)")
}
}
Just use the .sizeToFit method e.g:
func changeImageAgain(){
if let image = UIImage(named: "Octocat"){
dynamicImageView.image = image
dynamicImageView.sizeToFit()
print("Size Of ImageView = \(dynamicImageView.frame.size)")
}
}
Running on an iPhone 7 Plus the UIImageView size is initially (375,647), and when applying either method the size changes to (800,665) which is the same size as the GitHub OctoCat:
I have created one demo app which will contains XIB with image view. Here I have written code to resize imageView as per image size. This code is working in my project, I shared link for demo app, you can download and test it.
In this project, I have implemented code to create imageView based on image size and vertically and horizontally centred aligned.
Also implemented code to zoom image.
https://app.box.com/s/sro2g7gdv5c67f9oj97gfhicjnpo2vqc
I hope this will work for you and as per your requirement.
I have created extension for UITextView, where I set image to the textview using below code.
let backgroundImage = UIImageView(frame: ....)
backgroundImage.image = UIImage(named: "textarea.png")
self.addSubview(backgroundImage)
self.sendSubview(toBack: backgroundImage)
At start it looks fine as showing below image (left side), however as I add multi-line text, image also starts scrolling (right side), which is incorrect.
Any idea how to make image fixed as it is?
Below is what I did
let backgroundImage = UIImageView(frame: ....)
backgroundImage.image = UIImage(named: "textarea.png")
mainView.insertSubview(backgroundImage, belowSubview: self)
mainView is nothing but the main view (self.view) of view controller.
This is like we are not going to add backgroundImage in the subview of textview, instead we will be adding in the mainview (self.view).
A simple solution:
A container UIView contains background UIImageView and TextView(with clear background color).
I have an image view that gets a dynamic image, and uses aspect fit to determine the frame of the image itself. I am trying to constraint a view inside of the image view, but when the image dynamically resizes, the view inside of it doesn't also dynamically resize when my constraints are just set to the borders of the image view.
Is there any way to set the constraint to the content size that the image is after it resizes, inside interface builder?
Thank you!
When you set the image view’s frame, pick an initial height or width—not both. Then, get image.size and calculate the proportional other dimension. For example:
let width = 100
let height = width * image.size.width / image.size.height *
imageView.frame.size = CGSize(width: width, height: height)
imageView.contentMode = .scaleAspectFit
imageView.image = image
Now your imageView is exactly the size of your image. You can do the same thing with a fixed height by simply replacing “height” with “width” and vice versa in the code above.
Hope this is what you’re looking for.
I want to set background image for a label in my project but i dont know for how to set, please say it is possible or not?
This should do the trick:
yourLabel.backgroundColor = UIColor(patternImage: UIImage(named: "backgroundImage")!)
The UILabel control does not have background image or image property.
You can set image to UILabel using backgroundColor property as shown below :
cell.lblNumber!.text = ""
cell.lblNumber!.backgroundColor = UIColor(patternImage: UIImage(named: "OK")!)
Set the text to empty since it will display image and text on top of it.
I'm using storyboard and a TabBarNavigation Controller. The View Controller of the tabs are embedded in NavigationController. I didn't set the image of the first TabBarItem in the storyboard.
The png files don't show up. Only a gray square is shown, so maybe the size needs to be changed.
If I select the second tab the png file in the first tab will be shown in the correct size.
I couldn't figure that out with the documentation. How do I have to adjust the image or what do I have to do?
var tabBarItem1 : UITabBarItem
var image1 = UIImage(named: "feed.png")
var image2 = UIImage(named: "feed_chosen.png")
tabBarItem1 = tabBar.items[0] as UITabBarItem
tabBarItem1.title = "Feed"
image1.drawInRect(CGRect(x: 0, y: 0, width: 40, height: 30))
tabBarItem1.image = image1
tabBarItem1.selectedImage = image2
Here is a link to some info on how to customize UITabBarController tab images in iOS 7+:
http://appplusme.tumblr.com/post/68723979500/adding-a-custom-selected-uitabbaritem-image-in-ios7
And, of course, there's always Apple's docs:
https://developer.apple.com/library/ios/documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html