In iOS 13 UITabBarItem's standardAppearance is applied to all other items - ios

I'm trying to do a simple thing on iOS 13 - the last tab bar item should always be displayed in a different color.
I tried to use the new UITabBarItem.standardAppearance member. Here are my codes:
// first, set the default colors for the whole tab bar
let color = Style.Color.tabItem
let text = [NSAttributedString.Key.foregroundColor: color]
let selectedColor = Style.Color.tabItemSelected
let selectedText = [NSAttributedString.Key.foregroundColor: selectedColor]
let barAppearance = UITabBarItemAppearance()
barAppearance.normal.iconColor = color
barAppearance.disabled.iconColor = color
barAppearance.selected.iconColor = selectedColor
barAppearance.focused.iconColor = selectedColor
barAppearance.normal.titleTextAttributes = text
barAppearance.disabled.titleTextAttributes = text
barAppearance.selected.titleTextAttributes = selectedText
barAppearance.focused.titleTextAttributes = selectedText
tabBar.standardAppearance.stackedLayoutAppearance = barAppearance
tabBar.standardAppearance.inlineLayoutAppearance = barAppearance
tabBar.standardAppearance.compactInlineLayoutAppearance = barAppearance
tabBar.standardAppearance.backgroundColor = Style.Color.tabBar
// now, for the last item set special colors
if let lastItem = tabBar.items?.last {
let specialColor = Style.Color.tabItemSpecial
let specialText = [NSAttributedString.Key.foregroundColor: specialColor]
let specialSelectedColor = Style.Color.tabItemSpecialSelected
let specialSelectedText = [NSAttributedString.Key.foregroundColor: specialSelectedColor]
let itemAppearance = UITabBarItemAppearance()
itemAppearance.normal.iconColor = specialColor
itemAppearance.disabled.iconColor = specialColor
itemAppearance.selected.iconColor = specialSelectedColor
itemAppearance.focused.iconColor = specialSelectedColor
itemAppearance.normal.titleTextAttributes = specialText
itemAppearance.disabled.titleTextAttributes = specialText
itemAppearance.selected.titleTextAttributes = specialSelectedText
itemAppearance.focused.titleTextAttributes = specialSelectedText
let itemBarAppearance = UITabBarAppearance()
itemBarAppearance.stackedLayoutAppearance = itemAppearance
itemBarAppearance.inlineLayoutAppearance = itemAppearance
itemBarAppearance.compactInlineLayoutAppearance = itemAppearance
itemBarAppearance.backgroundColor = Style.Color.tabBar
lastItem.standardAppearance = itemBarAppearance
}
Expected behavior:
All the tab items are ALWAYS shown in tabItem/tabItemSelected colors, the last tab item is ALWAYS shown in tabItemSpecial/tabItemSpecialSelected color.
Actual behavior:
When any of the items is selected except the last one - all the tabs items are shown in tabItem/tabItemSelected, including the last one!
When I select the last tab bar item, it's standardAppearance style is applied TO ALL the other tab bar items too! So all of them use tabItemSpecial/tabItemSpecialSelected color scheme.
Am I doing something wrong? Or maybe I misunderstood the new APIs and there is no way to have one tab bar item to always have a different color?

This is the expected behavior. You aren't customizing how a specific tab bar item looks, you are customizing how the tab bar appears when that item is the currently selected one.

Related

ios Charts change grid lines color

I have made a small application measuring Heart Rate from iWatch. The chart is working as intended showing current heart rate, but I have several issues with the design. Below you can find the image of my chart and its setting in swift.
ios Chart
//chart set up
self.chtChart.delegate = self as? ChartViewDelegate
self.chtChart.noDataTextColor = UIColor.white
self.chtChart.noDataText = "Press Start button to display the chart"
let set_a: LineChartDataSet = LineChartDataSet(entries:[ChartDataEntry(x: Double(Int(0)), y: self.valueHR)], label: "BPM")
set_a.drawCirclesEnabled = false
set_a.setColor(UIColor.systemPink)
set_a.drawValuesEnabled = false
set_a.lineWidth = 5.0
//remove vertical grid and labels
self.chtChart.xAxis.drawGridLinesEnabled = false
self.chtChart.rightAxis.drawLabelsEnabled = false
self.chtChart.xAxis.drawLabelsEnabled = false
//change label 'BPM' color
self.chtChart.legend.textColor = UIColor.white
//change left numbers (bpm values)
self.chtChart.leftAxis.labelTextColor = UIColor.white
//change right border line color
self.chtChart.rightAxis.axisLineColor = UIColor.white
//change left border line to white
self.chtChart.leftAxis.axisLineColor = UIColor.white
//change values from decimal to int on left axis
let fmt = NumberFormatter()
fmt.numberStyle = .decimal
fmt.maximumFractionDigits = 0
fmt.groupingSeparator = ","
fmt.decimalSeparator = "."
self.chtChart.leftAxis.valueFormatter = DefaultAxisValueFormatter.init(formatter: fmt)
self.chtChart.data = LineChartData(dataSets: [set_a])
As you can see, my chart does not have the bottom border and I also can't change the color of the grid line inside the chart (I want to change it to white). I have tried following commands but nothing was changed:
self.chtChart.xAxis.axisLineColor = UIColor.white
self.chtChart.borderColor = UIColor.white
self.chtChart.xAxis.gridColor = UIColor.white
I was going through all possible attributes and found that adding the lines:
self.chtChart.xAxis.axisLineColor = UIColor.white
self.chtChart.rightAxis.gridColor = UIColor.white
solves the problem with the bottom border and also changes the color of the grid lines to white.
Edit: the bottom line problem still appears to be an issue. It does not show on the screen.
Edit2: adding the all borders at once and setting their color seems to solve the issue:
self.chtChart.drawBordersEnabled = true
self.chtChart.borderColor = UIColor.white

How to select tab bar item without selecting it

I am using swift and I have a problem with the tab bar. I want to select a tab bar item, but without open it, what I mean: I want to change its color like it is selected but without the selection is it possible to be done. Here is a image: https://imgur.com/a/IdPituS , I want the top left item (burger menu item) color to be like the first one and the first one color to be light (like not selected).
Try to edit the image of the item you want to highlight (selecting without selecting).
You can try something like this:
let image = shouldHighlight ? UIImage(named: "highlightItem") : UIImage(named: "normalStyle")
myViewController.tabBarItem = UITabBarItem(title: "text", image: image, selectedImage: UIImage(named: "selectedImage"))
Then you update the viewControllers property in you tab bar viewController
myTabViewController.viewControllers
You can pre-select the tabBar item
like this:
tabBarController?.selectedIndex = 0 //select whatever You want
and
colorTint the unselected index
tabBar = UITabBarController()
tabBar?.tabBar.barTintColor = UIColor.white //bar Color
tabBar?.tabBar.tintColor = .red //Icons color
tabBar?.tabBar.unselectedItemTintColor = .white //Unselected item Colors

Wrapping text on TitleLabel and DetailLabel - Material Card

I am using CosmicMind - Material Framework to create a Card. titleLabel and DetailLabel do not wrap to a new line.
My question is: What do I need to do to accomplish such task and is it supposed to be automatically adjusted by the framework?
This is what I have so far:
let card = Card()
var heartIcon = IconButton()
heartIcon = IconButton(image: Icon.favoriteBorder, tintColor: Color.red.base)
//Title Bar
let toolbar = Toolbar(rightViews: [heartIcon])
toolbar.title = cardData.cardTitleText
toolbar.titleLabel.textAlignment = .left
toolbar.titleLabel.numberOfLines = 0
toolbar.detail = "Company: " + cardData.cardTitleSubtitle
toolbar.detailLabel.font = RobotoFont.regular(with: 14)
toolbar.detailLabel.textColor = Color.grey.base
toolbar.detailLabel.textAlignment = .left
toolbar.detailLabel.numberOfLines = 0
And this is my output:
Update:
I managed to achieve what I wanted by increasing the size of the toolbar frame.
toolbar.frame = CGRect(x:0,y:0,width: view.frame.width,height: 100)
My goal was to at least show 2 lines of text.
Thanks!
I have no experience with the CosmicMind framework but usually, the label.numberOfLines property defines the maximum number of lines that the label text can have. You can set it to 2 instead of 0 and it should wrap.

How to set bar button item background image globally in iOS 13

iOS 13 has a whole new set of classes for configuring navigation bars and bar button items:
UIBarAppearance
UINavigationBarAppearance
UIBarButtonItemAppearance
UIBarButtonItemStateAppearance
How do I use these to give bar button items a global appearance? Let's say I want all bar button items to have a common background image. I used to say:
UIBarButtonItem.appearance().setBackgroundImage(im, for:.normal, barMetrics:.default)
This seems to be just what is superseded by these new classes. So what's the right way now?
It's verbose, but in some ways it's clearer. You can configure a navigation bar appearance and assign it to the navigation bar's standardAppearance through the proxy:
let app = UIBarButtonItemAppearance()
app.normal.backgroundImage = im
let navbarapp = UINavigationBarAppearance()
navbarapp.configureWithOpaqueBackground()
navbarapp.buttonAppearance = app
UINavigationBar.appearance().standardAppearance = navbarapp
The only problem with that is that it assigns the same background image to back button items. So if that's not desired, you have to code defensively, assigning back button items an empty (not nil) image:
let app = UIBarButtonItemAppearance()
app.normal.backgroundImage = im
let navbarapp = UINavigationBarAppearance()
navbarapp.configureWithOpaqueBackground()
navbarapp.buttonAppearance = app
let back = UIBarButtonItemAppearance()
back.normal.backgroundImage = UIImage() // prevent back button item
navbarapp.backButtonAppearance = back
UINavigationBar.appearance().standardAppearance = navbarapp

Display only text without image for UITabBarItem?

I want to display only title for tabbar item without image. Is it possible? When I tried setting
let tabItem = UITabBarItem()
tabItem.title = "Tab1"
tabItem.selectedImage = nil
tabItem.image = nil
tabViewController?.tabBarItem = tabItem
I'm just getting a blank space which is tapable, but no title or image.
simplest way is remove the images from the tab bar and set the custom offset vertical.as shown in the image. or else follow the link that TJ3n priveded remove the images in the UITabBarItem and aligned vertically the title.
In case you want it to be done programmatically:
tabItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -16)

Resources