iOS 11 - 导航栏大标题自定义偏移量

15

我能否改变大型导航栏标题的x轴偏移量?我想将其改为36pt。

导航栏

5个回答

32

我刚刚发现在最新版的iOS 12中,如果你简单地修改UINavigationBarlayoutMargins属性,这将影响到大标题。

let navigationBar = navigationController.navigationBar
navigationBar.layoutMargins.left = 36
navigationBar.layoutMargins.right = 36
我尝试了这里提到的解决方案,使用自定义的NSMutableParagraphStyle。确实有效,但由于它拉伸了大标题所用的UILabel视图,当向下滑动时,文本稍微增长的微妙动画变得相当扭曲。

2
非常好的提示。如果您正在使用搜索栏,这也适用:searchController.searchBar.layoutMargins.left = X - ruddfawcett
1
NSMutableParagraphStyle的方法问题,你提出了很好的观点。谢谢! - Kirill

9
您可以通过以下方式添加额外的偏移量:
if #available(iOS 11.0, *) {
    let navigationBarAppearance = UINavigationBar.appearance()
    let style = NSMutableParagraphStyle()
    style.alignment = .justified
    style.firstLineHeadIndent = 18
    navigationBarAppearance.largeTitleTextAttributes = [NSAttributedStringKey.paragraphStyle: style]
}

这个对于我的长标题出了问题。我加上了 style.lineBreakMode = NSLineBreakByTruncatingTail; - mangerlahn

1

你不能直接这样做。你需要编写自己的NavigationController,通过继承UINavigationController来实现。


0

SwiftUI

你可以在AppDelegate中调整整个应用程序的布局边距。

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil
    ) -> Bool {
        // Adjust left margin
        UINavigationBar.appearance().layoutMargins.left = 36
        
        return true
    }
}

@main
struct testApp: App {
    // Attach AppDelegate
    @UIApplicationDelegateAdaptor(AppDelegate.self) var delegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

0
你需要子类化UINavigationBar,然后重写draw方法,在里面进行更改。看一下我的工作示例,然后根据需要调整偏移量/样式:
    override func draw(_ rect: CGRect) {
    super.draw(rect)

    self.backgroundColor = UIColor.white
    let largeView = "_UINavigationBarLargeTitleView"
    let labelcolor = UIColor(red: 36.0/255.0, green: 38.0/255.0, blue: 47.0/255.0, alpha: 1.0)

    for view in self.subviews {
        if largeView.contains(String(describing: type(of: view))) {
            for v in view.subviews {
                if String(describing: type(of: v)) == "UILabel" {
                    var titleLabel = v as! UILabel
                    var labelRect = titleLabel.frame
                    let labelInsets = UIEdgeInsets(top: 10, left: 13, bottom: 0, right: 0)
                    let attrText = NSMutableAttributedString(string: "Jobs", attributes: [NSAttributedStringKey.font: UIFont(name: "SFProDisplay-Heavy", size: 30)!, NSAttributedStringKey.foregroundColor: labelcolor])

                    labelRect.origin.y += 20
                    let newLabel = UILabel(frame: labelRect)
                    newLabel.attributedText = attrText
                    titleLabel.text = ""
                    if labelRect.origin.y > 0 {
                        titleLabel = newLabel
                        titleLabel.drawText(in: UIEdgeInsetsInsetRect(labelRect, labelInsets))
                    }
                }
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接