I am working on an app in Xcode. I currently have three tabBarItems on my tab bar. I want the middle one to be a picture chosen by the user. I have the desired picture in a variable and i have the tabbar.swift set up to put the code in. I just need to set the image to the variables image with correct size (and make the picture appear as a circle) and the title name to a string.
Any help in doing this would be much appreciated. Thanks
Try this code.
self.tabBarController?.tabBar.items![0].image = UIImage(named: "your image name")
// items![0] index of your tab bar item.items![0] means tabbar first item
self.tabBarController?.tabBar.items![0].selectedImage = UIImage(named: "your image name")
Related
I'm writing a pretty simple line, but it's hard for me to see what I've actually written, because my image title is displayed as an icon in xcode.
I want to see it as:
thing.image = picture.png
But instead I see it as:
thing.image = an actual icon is here in the code for some weird purpose I can't understand
I've searched with no luck. Any help is greatly appreciated. Thank you!
**edit: I'm assuming it's an icon placeholder... it displays as a small white box.
**edit: here's a ss of how my code looks:
Related: I like the literals; to find out the name of the image after-the-fact, have Xcode reveal the name of the image asset by selecting the literal, then press Cmd-E, "Use Selection for Find". The image's name will show in the Find bar, e.g. #imageLiteral(resourceName: "icn_spinner")
Alternatively, press Cmd-/ to comment the line out; the commented form contains the literal text.
assign image to the UIImageView
thing.image = UIImage(named:"picture.png")
Highlight image and press Ctrl + F, then the image name will be in the toolbar.
Instead of using the tint color to indicate when a tab is selected, I just want to use the original images. These are the images I'm using:
Default:
Selected:
I added the images in the Storyboard, and in my code to setup the TabBarController I have the following:
let manageItem = tabBar.items?[1]
manageItem?.image?.imageWithRenderingMode(.AlwaysOriginal)
manageItem?.selectedImage?.imageWithRenderingMode(.AlwaysOriginal)
But every time I build and run, I'm still getting the blue color when selected. Also, it seems to be altering the selected image. Here's what it looks like:
Not selected (second tab):
Selected:
Why is it not using the original images?
As far as I recall imageWithRenderingMode returns new image, so you should rather use it like this :
let manageItem = tabBar.items?[1]
manageItem?.image = manageItem?.image?.imageWithRenderingMode(.AlwaysOriginal)
manageItem?.selectedImage = manageItem?.selectedImage?.imageWithRenderingMode(.AlwaysOriginal)
I am trying to replace the bar button items in my toolbar with images. I was able to select image in the attribute inspector. But now the text is replaced with image. Is there anyway i can keep both text and image (text should be below image)
What i have is this.
Please click to see image
What i am looking for is something like this
Please click to see image
Thanks for helping.
You can place the image in the selected image attribute
And you can place the text in the bar item title attribute
Something like this
This can be done when you do to the navigation controller for that particular tab.. Like in your case navigation controller for item 1
Hope that helps
I have set in Storyboard tab bar item to be Custom, Image to my outline image and Selected image to my filled image but the selected image does not show up when I run the app.
It works if I create a tab bar item programmatically using
UITabBarItem(title: String?, image: UIImage?, selectedImage: UIImage?)
I use Xcode 6.1.1.
What can be wrong?
I think, it is a bug of Xcode, if you add a user defined runtime attribute with type Image and keyPath "selectedImage", you'll can set your image.
Here example of the Tabbed Application template with a custom selected image on the second tab (I use a image from the first tab):
This is more than likely because it doesn't meet the interface requirements. Tab bar items are picky, the strokes or outlines for a selected image have to be noticeably greater than its counterparts. See here specifically the notes about the stroke width. Conditionally, if it's not a 'filled' in variation of your unselected image it has a chance of not populating depending on you create the selected image in comparison.
As of Xcode 9, the functionality to set a selected image is available (and works) in interface builder.
This is my setup for a tab bar item:
However when clicking on the item inside the app the 1051-id-badge-selected image is not shown, instead nothing is shown:
Is something wrong with my setup? Any ideas?
To get around this issue and set selected image without writing any code, we can also use "User Defined Run Attributes".
Select the tab bar item, then in 'Identity Inspector', add a new value from 'User Defined Runtime Attributes'.
Set the 'Key Path' to selectedImage, and choose 'Type' Image, then fill in the 'Value' with your image name.
If the inspector solution is not working for you (e.g. because of an Xcode bug) you can try this solution. In AppDelegate:
var tabBarController = self.window!.rootViewController as UITabBarController
let tabItems = tabBarController.tabBar.items as [UITabBarItem]
tabItems[2].selectedImage = UIImage(named: "1051-id-badge-selected.png")
Update on May 25, 2016:
On Xcode 7.3.1 the Selected Image field under Tab Bar Item section is working correctly. (But I don't know from which exact version Apple fixed it for Xcode).