ios 14 uisplitviewcontroller 5您可能会遇到的问题

The final build of iOS 14.0 will be released soon, and it’s about time to make sure our apps are running great on iOS 14. This year Apple introduced major changes in UISplitViewController, which are mostly dedicated to a new three-column layout and a sidebar. While the new sidebar UI looks great, in this post I will only focus on changes to existing functionality that we already use in our apps and show what can go wrong when your existing app is run on iOS 14. With that said let’s dive in.

iOS 14.0的最终版本即将发布,现在是时候确保我们的应用程序在iOS 14上运行良好。今年,Apple在UISplitViewController中引入了重大更改,这些更改主要用于新的三列布局和侧边栏。 尽管新的侧边栏UI看起来不错,但在本文中,我将仅着重于对我们已在应用程序中使用的现有功能的更改,并说明当您的现有应用程序在iOS 14上运行时可能出问题的地方。

新样式属性以及为什么存在未指定的样式类型 (New style property and why there is an unspecified style type)

Probably the first thing that you’ll notice when looking at the UISplitViewController API exclusive to iOS 14 will be a new initializer that accepts UISplitViewController.Style as a single argument. While doubleColumn and tripleColumn styles are fairly self-explanatory, there is a third mysterious option called unspecified. After some testing, it became clear that unspecified style is a way to tell UIKit that you want iOS 13 behavior, so if you don’t want to deal right now with all the issues described next, you can enjoy the old behavior even when running on iOS 14, if you’ll use the unspecified style. The only issue with this scenario is that it is a bit tricky to set this specific style. Next, I’ll quickly summarize solutions that do and don’t work for such a task.

在查看iOS 14专有的UISplitViewController API时,您会注意到的第一件事可能是一个新的初始化程序,该初始化程序接受UISplitViewController.Style作为单个参数。 虽然doubleColumnTripleColumn样式相当不言自明,但第三个神秘选项称为unspecified 。 经过一些测试,很明显, 未指定的样式是一种告诉UIKit您想要iOS 13行为的方式,因此,如果您现在不想处理接下来描述的所有问题,即使在运行时也可以享受旧的行为在iOS 14上,如果您使用未指定的样式。 这种情况的唯一问题是,设置此特定样式有点棘手。 接下来,我将快速总结在此任务上可行或不可行的解决方案。

什么不起作用 (What does not work)

  1. Calling new initializer with unspecified style. The most obvious approach, however, it triggers runtime exception with a message

    未指定的样式调用新的初始化器。 但是,最明显的方法是通过消息触发运行时异常

API misuse. -initWithStyle: may not be used with UISplitViewControllerStyleUnspecified.

API滥用。 -initWithStyle:不可与UISplitViewControllerStyleUnspecified一起使用。

2. Performing setStyle: selector. Leads to exception too with a message

2.执行setStyle:选择器。 一条消息也导致异常

SPI misuse. -setStyle: should be used by IB to set up UISplitViewController for column-style behavior using UISplitViewControllerStyleDoubleColumn or -TripleColumn (yep, there is a typo in that message).

SPI滥用。 -setStyle:IB应使用IB来使用UISplitViewControllerStyleDoubleColumn或-TripleColumn设置UISplitViewController的列样式行为(是的,该消息中有错字)。

什么有效 (What does work)

  1. If you instantiate your UISplitViewController in code, just call the default NSObject initializer: UISplitViewController().

    如果您在代码中实例化UISplitViewController ,则只需调用默认的NSObject初始化程序: UISplitViewController()即可

  2. Thankfully, there is also a solution for those who instantiate their UISplitViewController from the storyboard. In the 5th beta of Xcode 12, Apple allowed us to set the unspecified style directly from the Interface Builder.

    值得庆幸的是,对于从情节提要实例化其UISplitViewController的用户 ,还有一个解决方案。 在Xcode 12的第5个Beta中,Apple允许我们直接从Interface Builder设置未指定的样式。

Image for post

Want to use the modern API instead? Great, stay tuned to see what breaks as soon as you’ll pick the doubleColumn style.

是否要使用现代API? 太好了,敬请期待 ,一旦您选择doubleColumn样式,便会发现有什么突破。

裁剪的主视图控制器的视图 (Clipped master view controller’s view)

After choosing the doubleColumn style in Interface Builder and running my app I noticed that for some reason the whole master view controller was clipped.

在Interface Builder中选择doubleColumn样式并运行我的应用程序后,我注意到由于某种原因整个主视图控制器都被裁剪了。

Image for post

The layout of the main menu view controller was created years ago when Safe Area layout guides were not present. Hence, the collection view was constrained to its superview, instead of Safe Area, which caused this weird clipping.

主菜单视图控制器的布局是多年前没有安全区域布局指南时创建的。 因此,集合视图被约束为其超级视图,而不是安全区域,从而导致这种奇怪的裁剪。

Image for post

After enabling Safe Area layout guides in File inspector everything went back to normal.

在文件检查器中启用“安全区域”布局指南后,一切恢复正常。

As Geoff Hackworth explained in his Tweet the primary view controller is wider than its visible area to support the edge swipe when the primary view controller is hidden. Make sure you have “Show Device Bezel” enabled in Simulator to perform the edge swipe in it.

正如Geoff Hackworth在其Tweet中所述,主视图控制器的宽度大于其可见区域,以在隐藏主视图控制器时支持边缘滑动。 确保已在模拟器中启用“显示设备挡板”以在其中执行边缘轻扫。

DisplayModeButtonItem不再可访问 (DisplayModeButtonItem is no longer accessible)

After navigating a bit through the app I noticed the following message in the console

导航了一下应用程序后,我在控制台中注意到以下消息

[Assert] displayModeButtonItem is internally managed and not exposed for DoubleColumn style. Returning an empty, disconnected UIBarButtonItem to fulfill the non-null contract.

[声明] displayModeButtonItem由内部管理,不针对DoubleColumn样式公开。 返回一个空的,断开连接的UIBarButtonItem来履行非空合同。

This message showed up right after I tried to set leftBarButtonItem of a detail view controller, which was about to be presented:

在尝试设置显示的详细信息视图控制器的leftBarButtonItem之后,此消息立即显示:

viewController.navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem
viewController.navigationItem.leftItemsSupplementBackButton = true

Previously this solution allowed me to show a button at the detail view controller that could expand the view controller to the full width of an iPad.

以前,该解决方案使我能够在详细视图控制器上显示一个按钮,该按钮可以将视图控制器扩展到iPad的整个宽度。

However, on iOS 14 similar functionality is provided by default with a new button displayed on top of master and detail view controllers, so the legacy solution should be executed only on older iOS versions.

但是,在iOS 14上,默认情况下提供了类似的功能,并且在主视图和局部视图控制器顶部显示了一个新按钮,因此旧式解决方案应仅在较旧的iOS版本上执行。

过渡期间展开和折叠按钮的淡色 (Tint color of expand & collapse button during the transition)

If you look closely at the last gif, you’ll probably notice that the color of the button changes to standard iOS blue while the transition is going on while UINavigationBar.appearance().tintColor is used at other times. After inspecting the view hierarchy it turned out that there are 3 of such buttons.

如果仔细查看最后一个gif,您可能会注意到,在进行过渡时,其他时间使用UINavigationBar.appearance()。tintColor时,按钮的颜色变为标准iOS蓝色。 检查视图层次结构后,结果发现有3个这样的按钮。

Image for post

Most likely one of them is shown only when the transition is in progress. After trying several approaches I figured out that the color of that button is defined by UISplitViewController’s view tint color.

仅当过渡进行时才显示其中之一。 在尝试了几种方法之后,我发现该按钮的颜色由UISplitViewController的视图色调颜色定义。

非工作委托回调 (Nonworking delegate callback)

After fixing previous flaws I decided to see if everything is good when the app is run on an iPhone. Unfortunately, after launching the app I saw the placeholder view controller instead of the main menu and a message in the console saying

修复了先前的缺陷后,我决定查看在iPhone上运行该应用程序是否一切正常。 不幸的是,启动应用程序后,我看到了占位符视图控制器,而不是主菜单,并且在控制台中显示了一条消息,提示

[UISplitViewController] Skipping delegate callback, splitViewController:collapseSecondaryViewController:ontoPrimaryViewController:. Unsupported for UISplitViewController style DoubleColumn

[UISplitViewController]跳过委托回调,splitViewController:collapseSecondaryViewController:ontoPrimaryViewController:。 UISplitViewController样式DoubleColumn不支持

Image for post
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
    if let navigationController = secondaryViewController as? EEHNavigationController, navigationController.topViewController is PlaceholderViewController {
        return true
    } else {
        return false
    }
}

Thankfully, there is a new callback introduced in iOS 14, which we can use for the very same purpose:

值得庆幸的是,iOS 14中引入了一个新的回调,我们可以将其用于相同的目的:

@available(iOS 14.0, *)
func splitViewController(_ svc: UISplitViewController, topColumnForCollapsingToProposedTopColumn proposedTopColumn: UISplitViewController.Column) -> UISplitViewController.Column {
    return .primary
}

Still, the warning about skipped delegate callback will be printed in your console (assuming that you’ve kept the previous callback to support the pre-iOS 14 systems).

仍然会在控制台中显示有关跳过委托回调的警告(假设您保留了以前的回调以支持iOS 14之前的系统)。

最后的想法 (Final thoughts)

Overall I’m glad that this year’s migration to the latest iOS turned out to be not complicated at all. Hope you’ll update your apps without issues as well. That’s all, and thanks for reading!

总的来说,我很高兴今年迁移到最新的iOS并不复杂。 希望您也能更新自己的应用程序而不会出现问题。 仅此而已,感谢您的阅读!

翻译自: https://medium.com/swlh/ios-14-uisplitviewcontroller-5-issues-that-you-may-run-into-65b09601b3fb

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值