iOS tabbar item ignoring title position - ios

I would like to add some space below the tabbar item titles. So I have tried first using storyboards:
The changes are reflected in the storyboard but not at running time in the simulator.
I have tried also programmatically:
UITabBarItem.appearance().titlePositionAdjustment.vertical = -2
or:
tabBar.items!.forEach {
$0.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
}
but also it does not work.
Do you know if I could have some missconfiguration?

Sorry, I didn´t realize that I had some appearance code overriding my code. You was right:
let appearance = tabBar.standardAppearance
appearance.configureWithOpaqueBackground()

Related

UITabBar transparent labels bug in iOS 13

iOS: 13.1.2
Xcode: 11.1 (11A1027)
In our tab bar we opted for using transparent text for the tab items, so in iPhone we only show the tab item image, while the text is invisible (and it should be only visible on iPad), we do this by calling:
extension UITabBarItem {
func updateTitleVisibility(for traitCollection: UITraitCollection) {
switch traitCollection.horizontalSizeClass {
case .compact:
hideTabBarTitle()
default:
showTabBarTitle()
}
}
func hideTabBarTitle() {
imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .normal)
setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.clear], for: .selected)
}
func showTabBarTitle() {
imageInsets = .zero
setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.licorice], for: .normal)
setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.secondaryBlue], for: .selected)
}
}
When compiling our app for iOS 13 (it didn’t happen on iOS 12), there is a strange behavior happening (notice the tab bar):
Video of the bug # Imgur
(^ i didn't manage to embed it in the post)
So the tab text sudden shows for the inactive tabs after presenting a full screen view controller, but incredibly enough when checking the view debugger the labels that are supposed to be transparent are indeed transparent
Has anybody seen a behavior like that? How can I fix it
Well, this is due to the behavior in the default dark mode on iOS 13.
To achieve what you wanted with labels as on iOS versions below 13,
Simply add this into your Info.plist:
<key>UIUserInterfaceStyle</key>
<string>Light</string>
What this essentially does is changing the global user interface style to the light style which is the default style on iOS versions below 13.
If you won't prefer to change the user interface style, you can also change the tint color of the unselected items on the tab bar:
tabBar.unselectedItemTintColor = .darkGray
or to any other tint color of your choice.

Get the default height (or maxY) of a NavigationBar on iOS

I would like to know if is there any way for us to obtain the default Navigation Bar height (maxY preferably) of a NavigationController.
Knowing that iOS 11 introduced large titles, is there any way for us then to get the default height (or maxY) of a Navigation Bar with a "small title" and of a Navigation Bar with a "large title"?
The reason I am asking this is because I am making the Navigation Bar's background transparent and introducing my own background to it (which is an Effect View). But the problem I am having is that every time I run the following code
self.navigationController?.navigationBar.frame.maxY
it returns a number ways higher than the expected :/
I tried to run this piece of code on many callbacks -> onViewWillAppear, onViewDidAppear, onViewDidLoad
You can get the height of navigation bar and status bar using this
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let topSpace:CGFloat?
if #available(iOS 11.0, *) {
topSpace = self.view.safeAreaInsets.top
} else {
topSpace = self.topLayoutGuide.length
}
print(topSpace)
}
I have used the native method to get the height of navigation bar including status bar. Use this line of code to get the navigation bar height and use as per your requirement. This worked for me perfectly fine on all devices & different iOS versions.
let navigationBarHeight = UIApplication.shared.statusBarFrame.size.height +
(self.navigationController?.navigationBar.frame.height ?? 0.0)
The best approach I found so far, without having to create a navigation controller instance:
[self.navigationBar sizeThatFits:CGSizeZero].height;
And just to mention, it supports screen orientation change too.
This works for me
let navigationBarHeight = UIApplication.shared.statusBarFrame.size.height +
(self.navigationController?.navigationBar.frame.height ?? 0.0)
Even tough your method may be the best solution for you I mostly try to not use the native navigation bar but hide it and create my own instead. This makes it easier to use custom and more advanced designs in the application.

LeftBarButtonItem with UISearchBar is not visible on iOS 11

The title is pretty self explanatory, on iOS 10.3 using a UIBarButtonItem with a custom view (in this case UIStackView) assigned to a LeftBarButtonItem of a NavigationBar is not visible on iOS 11. I haven't figure out why it is not showed but when I type something with the keyboard my logic of the TextChanged event works! So the UISearchView is there but it is not visible:
Here is some code (It is coded with C# but it is using Objective C methods.):
var width = NavigationController.NavigationBar.Frame.Width;
var height = NavigationController.NavigationBar.Frame.Height;
_searchBarContainer = new UIStackView(new CGRect(0, 0, width * 0.75, height))
{
Alignment = UIStackViewAlignment.Center,
Axis = UILayoutConstraintAxis.Horizontal,
Spacing = 3
};
_uiSearchBar = new UISearchBar
{
BackgroundColor = UIColor.Clear,
BarTintColor = UIColor.Clear,
BackgroundImage = new UIImage(),
Placeholder = Strings.Search
};
_uiSearchBar.SizeToFit();
if (_iOS11)
{
_uiSearchBar.HeightAnchor.ConstraintEqualTo(44).Active = true;
}
_searchbarButtonItem = new UIBarButtonItem(_searchBarContainer);
NavigationItem.SetLeftBarButtonItem(_searchbarButtonItem, true);
ParentViewController.NavigationItem.LeftBarButtonItem = NavigationItem.LeftBarButtonItem;
Using the same code on iOS 10 this works.
Please try out setting up constraints to size properly your _searchBarContainer before setting it as the left bar button item. From iOS11 navigations bars use auto layout. Make sure you only add the constraints if iOS 11 is present, I was having problems in iOS 9 navigation bars otherwise.
Also checkout this thread in the Dev forum where it's explained how the bar items are wrapped inside stack views, maybe also helps with your particular issue.

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)

Is there a way to change the text position in UITabBar or UITabBarItem?

This is a custom tab bar I intended to put up on the screen. However, my partner want the text to be slightly up. How can I do so?
Why don't you just have an empty title property for your view controller and add the title to your custom images for the tab?
UPDATE: For the sake of completeness of answer; from comments and ios tabbar put text in the middle when no image
[tab.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -10)]
You probably want to apply this offset globally, so I'd suggest
Obj-C
[UITabBarItem appearance].titlePositionAdjustment = UIOffsetMake(0, -4);
Swift:
UITabBarItem.appearance().titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -4)
Here's the same trick with Interface builder:
You can do it directly from Interface Builder (Custom Offset in Title Position), like this:
For iOS 13 and later:
if #available(iOS 13, *) {
let appearance = UITabBarAppearance()
appearance.stackedLayoutAppearance.selected.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10)
appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -10)
}
I've found the answer here: ios tabbar put text in the middle when no image will try this one and see how it goes :)
Update: It works. Only 1 line of code.

Resources