xcode 11 beta中这种新的导航栏行为是错误还是故意的?

时间:2019-06-06 18:07:19

标签: swift navigationbar xcode11

在Xcode 11 beta中编译了我的一个应用程序后,我注意到设置prefersLargeTitles时导航栏没有背景。这是预期的行为吗?

我注意到这是向下滚动时消息应用程序现在的工作方式,可见一个大标题,没有导航栏背景。

以下是用于设置navBar属性的代码:

 override func viewWillAppear(_ animated: Bool) {
    let textAttributes = [NSAttributedString.Key.foregroundColor:ThemeManager.shared.default1]
    self.navigationController?.navigationBar.largeTitleTextAttributes = textAttributes
    self.navigationController?.navigationBar.titleTextAttributes = textAttributes
    self.navigationController?.navigationBar.tintColor = ThemeManager.shared.default1
 self.navigationController?.setNavigationBarHidden(false, animated: true)
    self.navigationController?.navigationBar.prefersLargeTitles = true
    let nav = self.navigationItem
    nav.title = "My Profile"
}

以下是一些显示差异的图片:

左,在Xcode 10上编译,右,在Xcode 11 beta上编译:

enter image description here enter image description here

在11 Beta版本上滚动后,背景会逐渐消失。请注意,未在Xcode 11 Beta中编译的应用程序仍将以正常方式运行,只有在出于某些原因进行编译后才会更改。这是故意的,我该如何恢复原始行为?

2 个答案:

答案 0 :(得分:2)

这是iOS 13的预期行为。

Apple的想法(在我看来很糟糕)是标题应与内容合并以显示其相关性。一旦开始滚动,当内容移到标题栏后面时,标题栏将显示为“正确”外观。

之所以如此糟糕,是因为每个人当前都在计划所有UI,而没有这种行为。因此,新的行为应该是选择加入,而不是强迫所有人选择退出(即,更改会破坏每个人的代码,并且如果您要破坏每个人的代码,至少您应该清楚如何保持已尝试和真实的行为。最近十年)。

与您的情况一样,结果看起来很可怕。在我看来,结果也很可怕。

Apple没有给出答案,但说您应该使用

- scrollEdgeAppearance

从UINavigationBar开始,以便在将内容的顶部对齐到导航栏的底部时控制栏的外观...在我的情况下,此方法返回nil,所以我目前不确定如何应该使用这个。

这似乎也在这里讨论:

New UINavigationBar appearance in detail pane of UISplitViewController in iOS 13

因此,当前的解决方法似乎是您的视图控制器中的方法:

- (void)viewDidLoad;
{
    [super viewDidLoad];
    if (@available(iOS 13,*)){
        UINavigationBar *bar =self.navigationController.navigationBar;
        bar.scrollEdgeAppearance = bar.standardAppearance;
    }
}

它可以工作,但是如果这是预期的方法,我不知道...

编辑:

这样做似乎确实阻止了对UINavigationBar的任何其他直接自定义,如前所述。从这里调整scrollEdgeAppearance是可行的方法。丑陋。丑陋。丑。

编辑:进度...现在正在管理后台。您需要调用此方法,而不是直接设置barTint。

@interface UINavigationBar (Compatibility)
- (void)setCompatibleTint:(UIColor *)fg andBarTint:(UIColor *)bg;
@end

@implementation UINavigationBar (Compatibility)
- (void)setCompatibleTint:(UIColor *)fg andBarTint:(UIColor *)bg;
{
    self.tintColor = fg;
    self.barTintColor = bg;
    if (@available(iOS 13,*)){
        // we need to tell it to adopt old style behavior first
        UINavigationBarAppearance *appearance = self.standardAppearance;
        appearance.backgroundColor = bg;
        NSDictionary *attributes = self.titleTextAttributes;
        appearance.titleTextAttributes = attributes;
        attributes = self.largeTitleTextAttributes;
        appearance.largeTitleTextAttributes = attributes;
        self.scrollEdgeAppearance = appearance;
        self.standardAppearance = appearance;
        self.compactAppearance = appearance;
    }
}
@end

我还不确定文本属性,但是似乎是背景色。这是完整的PITA。

将其设置为子类并覆盖barTint会更好,但是当然,很多UIKit对象自己创建了这些条,因此您不会获得子类。

答案 1 :(得分:1)

dbquarrel解决方案的快速版本:

override func viewDidLoad() {
    super.viewDidLoad()
    if #available(iOS 13.0, *) {
        let bar = self.navigationController?.navigationBar
        bar?.scrollEdgeAppearance = bar?.standardAppearance
    } else {
        // Fallback on earlier versions
    }
}