ios 11 UITabBar UITabBarItem定位问题 [英] ios 11 UITabBar UITabBarItem positioning issue

查看:1108
本文介绍了ios 11 UITabBar UITabBarItem定位问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用ios 11的新Xcode 9 beta构建了我的应用程序。我发现UITabBar存在问题,其中的项目通过UITabBar传播,标题与图像右对齐。我已经尝试更改代码以使其工作但仍然没有成功。



ios 10+





ios 11





<我可以使用 tabBarItem.titlePositionAdjustment
更改标题的位置但这不是我的要求,因为它应该自动显示在图像本身之下。我尝试将 tabbar.itemPositioning设置为UITabBarItemPositioningCentered ,并尝试更改 itemSpacing width ,但仍然无效。有人可以帮我理解为什么会这样,以及如何解决这个问题?我想要它喜欢ios 10+版本,图像是从iPad的最左边角落拍摄的。

解决方案

我在维护一个大型的iPad应用程序,主要使用Objective-C编写,在几个iOS版本中幸存下来。对于一些标签栏,我遇到了需要iOS 11前标签栏外观(标题上方的图标而不是旁边的图标)的情况。我的解决方案是创建一个UITabBar的子类,它覆盖 traitCollection 方法,以便它总是返回一个水平紧凑的特征集合。这会导致iOS 11在所有标签栏按钮的图标下方显示标题。



为了使用它,请设置标签栏的自定义类。故事板到这个新的子类并更改代码中指向标签栏的任何出口是这种新类型(不要忘记导入下面的头文件)。



.h文件在这种情况下非常空:

  // 
// MyTabBar.h
//

#import< UIKit / UIKit.h>

@interface MyTabBar:UITabBar

@end

这是带有 traitCollection 方法实现的.m文件:

  // 
// MyTabBar.m
//

#importMyTabBar.h

@implementation MyTabBar

//在iOS 11中,UITabBarItem在水平常规环境中拥有图标右侧的标题
//(即iPad)。为了使标题保持在图标下方,有必要继承UITabBar并覆盖
// traitCollection以使其水平紧凑。

- (UITraitCollection *)traitCollection {
return [UITraitCollection traitCollectionWithHorizo​​ntalSizeClass:UIUserInterfaceSizeClassCompact];
}

@end


I have built my app using new Xcode 9 beta for ios 11. I have found an issue with UITabBar where items are spread through the UITabBar and title is right aligned to the image. I have tried changing the code to get it to work but still not successful.

ios 10+

ios 11

I could change the position of title using tabBarItem.titlePositionAdjustment But that is not my requirement as it should automatically come bellow the image itself. I tried setting tabbar.itemPositioning to UITabBarItemPositioningCentered and also tried changing itemSpacing and width, but still did not work. Can someone help me understand why this happens and how to fix this? I want it to like ios 10+ version and images are taken from the left most corner of an iPad.

解决方案

I am maintaining a large iPad app written mostly in Objective-C that has survived several iOS releases. I ran into the situation where I needed the pre-iOS 11 tab bar appearance (with the icons above the titles instead of next to them) for a couple tab bars. My solution was to create a subclass of UITabBar that overrides the traitCollection method so that it always returns a horizontally-compact trait collection. This causes iOS 11 to display the titles below the icons for all of the tab bar buttons.

In order to use this, set the custom class of the tab bars in the storyboard to this new subclass and change any outlets in the code that point to the tab bars to be of this new type (don't forget to import the header file below).

The .h file is pretty much empty in this case:

//
//  MyTabBar.h
//

#import <UIKit/UIKit.h>

@interface MyTabBar : UITabBar

@end

Here is the .m file with the implementation of the traitCollection method:

//
//  MyTabBar.m
//

#import "MyTabBar.h"

@implementation MyTabBar

// In iOS 11, UITabBarItem's have the title to the right of the icon in horizontally regular environments
// (i.e. the iPad).  In order to keep the title below the icon, it was necessary to subclass UITabBar and override
// traitCollection to make it horizontally compact.

- (UITraitCollection *)traitCollection {
    return [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
}

@end

这篇关于ios 11 UITabBar UITabBarItem定位问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆