Tab Bar - custom images without titles - swift

I want to set my tab bar to have custom images but XCode insists in leaving the space for the tab bar item title text leaving my images positioned too high in the tab bar. I've tried the following code in my TabBarController.swift viewDidLoad function:
let tabItems = tabBar.items as [UITabBarItem]
tabItems[0].title = nil
tabItems[0].selectedImage = UIImage(named: "HomeWhiteIcon")
tabItems[1].title = "Database"
tabItems[1].selectedImage = UIImage(named: "Second")
tabItems[2].title = nil
tabItems[2].selectedImage = UIImage(named: "SettingsWhiteIcon")
tabItems[3].title = nil
tabItems[3].selectedImage = UIImage(named: "ReportsWhiteIcon")
However, although no title is displayed the images are positioned too high as pic below (please ignore database icon - I have not set this icon yet.

Here's a random stab as swift is not my strong suit, but perhaps the database icon having a title is causing the entire title row to not collapse. Try removing the title from the database icon.
Otherwise have you seen the setTitlePositionAdjustment method? Perhaps adjust the title's position up into the icon and the space below will go away.

Related

How to programmatically change Tab Bar Item image and font when selected?

I would like to change the weight of my Tab Bar Item and font when it is selected. I assume this can be done by changing the Tab Bar Item Image and text when the associated tab is being selected. Apart from adjusting the tintcolor of highlighted tab bar items, I can't seem to find any information customising selected Tab Bar Items online even though most apps (including Instagram) do it. How is this done?
My current 5 tab bars are created in a UITabBarController Class and follow an almost identical formula like this:
let homeController = HomeViewController()
homeController.tabBarItem = UITabBarItem(title: "Home", image: UIImage(named: "home"), tag: 1)
let nav1 = UINavigationController(rootViewController: homeController)
viewControllers = [nav1, nav2, nav3, nav4, nav5]
Programmatically set selected UITabbarItem's image:
let tabBarItem = UITabBarItem(title: "title", image: UIImage(named: "defaultImage"), selectedImage: UIImage(named: "selectedImage"))
You can't as easily set selected UITabbarItem's font though. You'd need to create your own UITabBarController as shown in this SO thread.
In order to change the image I simply enter this code in the associated view controller class (in my case they are individuals swift files)
so in viewDidLoad of class HomeViewController: UIViewController {
self.tabBarItem.selectedImage = UIImage(named: "yourImageName")

Changing background colour of each tab in custom tab bar

How can I change the background colour for each tab in an iOS application in Swift, as shown in the image?
This can be implemented with the help of setting images on each tab:
let aryTabImages = ["home-icon.png","help-icon.png","photos-icon.png","cart-icon.png","search-icon.png"]
Assign to each tabbar item:
for (index, item) in self.tabBarController!.tabBar.items!.enumerate() {
item.image = UIImage(named: aryTabImages.objectAtIndex(index) as! String)!.imageWithRenderingMode(.AlwaysOriginal)
item.selectedImage = UIImage(named: aryTabImages.objectAtIndex(index) as! String)!.imageWithRenderingMode(.AlwaysOriginal)
}
Also Make sure about:
Tabbar item height for each IOS device
Tint color correctly selected of UITabbarController's tabbar.

cocoa - what happen when close a window (by pressing the red X button)?

I'm writing an app for MacOS to display a string to a NSTextfield in a window when user click on the button in the status bar menu (application is an agent). The thing is I want to use the NSScrollView to embrace that NSTextview so that the NSTextview is scrollable. It sound simple but some thing happen:
(1) When I click the button on the menu, the window appear using the NSWindowController.showWindow(). But if the string is large (larger than the clipview), the NSScrollView isn't scrollable. If I select the text inside, then I can select the rest of the text that doesn't appear in the clipview
(2) If I leave the window there and click to the button again, then the the text is scrollable and the scrollbar appear!
(3) If I close the window by click on the red X button on top-left, then click to the button, then the window appear with the same situation at first (isn't scrollable). Notice that I don't recreate the new instance of NSWindowController of the window, at this time I just do the showWindow() with the old instance of the NSWindowController
This is the code:
if(newWC.window == nil){
let storyboard = NSStoryboard(name: "Main", bundle: nil)
newWC = storyboard?.instantiateControllerWithIdentifier("newWC") as! NewWindowController
newWC.window?.level = kCGStatusWindowLevelKey
println("new window")
}
newWC.showWindow(self)
//This is the ViewController contain the NSScrollView and the TextField
manualCopyView = newWC.window?.contentViewController as! ManualCopyViewController
var theScrollView = manualCopyView.theScrollView
var manualTextField = manualCopyView.manualEditTextField
//Calculate height of a block of string
manualTextField.stringValue = totalString.stringByAppendingString("")
var textFieldHeight = (manualTextField.cell() as! NSTextFieldCell).cellSizeForBounds(NSMakeRect(0, 0, 448, CGFloat(FLT_MAX))).height
if(textFieldHeight < 200){
textFieldHeight = 200
}
var frame = manualCopyView.manualEditTextField.frame
var clipViewFrame = theScrollView.contentView.frame
frame.size.height = textFieldHeight
//Change the frame of the textfield to fit the content string
manualTextField.frame = frame
//Gain focus to the new window
NSApp.activateIgnoringOtherApps(true)
It look like when the window first appear (or re-appear), the NSTextField is shrink even that I changed it's frame. But if I don't close it and show the window again, the frame expand as expected
I also used auto layout with constraints in the width and height of the scrollview and the width and minimum height (>= 200) of the textfield (because I think the height would be expanded)
Another question in this case: I have to set the scrollview Vertical Elasticity to No Elasticity or even the second case won't work. Is that normal?
Thank for any help.

Swift, UitextField background with image on rightmost side

I have one UItextField and I want to show the date image inside a textfield in the rightmost side.
Below is my code
txtEffectiveDate.rightView = UIImageView(image: UIImage(named:"sorting-selected-22pt" )) // named: "sorting-selected-22pt")
But somehow it is not displaying in the UI page.
Let me know what i am Doing wrong?

Center Tab Bar Icons Without Text in Swift

we're trying to center the tab bar icons to the tab bar center because we don't want to have the text below. So the icons should be alone and centered in the tab bar. This is how they look now. The tab bar controller is not the root viewcontroller, so we can't access it directly using rootviewcontroller as many responses we've found out there. Any ideas? We're turning crazy...
To remove titletext, you can use code:
let tabBarItems = tabBar.items! as [UITabBarItem]
tabBarItems[0].title = nil
To get center of icon tabs, use this code
let tabBarItems = tabBar.items! as [UITabBarItem]
tabBarItems[0].imageInsets = UIEdgeInsetsMake(6,0,-6,0)
for complete code remove titletext and get center icon
let tabBarItems = tabBar.items! as [UITabBarItem]
tabBarItems[0].title = nil
tabBarItems[0].imageInsets = UIEdgeInsetsMake(6,0,-6,0)