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

Pet*_*ert 8 navigationbar swift 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"
}
Run Code Online (Sandbox Code Playgroud)

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

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

在此处输入图片说明 在此处输入图片说明

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

dbq*_*rel 8

这是iOS 13的预期行为。

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

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

与您的情况一样,结果看起来很糟糕。就我而言,结果看起来也很可怕。

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

- scrollEdgeAppearance
Run Code Online (Sandbox Code Playgroud)

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

这似乎也在这里讨论:

iOS 13中UISplitViewController的详细信息窗格中的新UINavigationBar外观

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

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

它有效,但是如果这是预期的方法,我不知道...

编辑:

如上所述,这样做似乎确实阻止了对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
Run Code Online (Sandbox Code Playgroud)

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

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


Pet*_*ert 6

dbquarrel解决方案的Swift版本。

首先声明您的textAttributes:

let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red]
Run Code Online (Sandbox Code Playgroud)

在中使用这些UINavigationBarAppearance()字符可使您以3种不同模式(scollEdge,标准和紧凑)更改文本的颜色。

override func viewDidLoad() {
    super.viewDidLoad()
    if #available(iOS 13.0, *) {
        let appearance = UINavigationBarAppearance()
        appearance.largeTitleTextAttributes = textAttributes
        appearance.titleTextAttributes = textAttributes
        let bar = self.navigationController?.navigationBar
        bar?.scrollEdgeAppearance = appearance
        bar?.standardAppearance = appearance
        bar?.compactAppearance = appearance
    } else {
        // Fallback on earlier versions
    }
}
Run Code Online (Sandbox Code Playgroud)