简体   繁体   English

在 swift 中将内容的前导边距设置为导航栏大标题的前导

[英]setting leading margin of content to the leading of navigation bar large title in swift

I have a question about aligning content using autolayout.我有一个关于使用自动布局对齐内容的问题。

I have a large navigation bar and one UIView right below the navigation bar.我有一个大导航栏和导航栏正下方的一个 UIView。 I want to set the UIView's leading anchor to the same leading anchor as the navigation bar.我想将 UIView 的前导锚设置为与导航栏相同的前导锚。 Currently, my code is something below and the view's leading anchor is not the same as the navigation bar's leading.目前,我的代码在下面,视图的前导锚点与导航栏的前导不同。 (yes, because I'm using layoutMarginsGuide so that's why.) (是的,因为我正在使用 layoutMarginsGuide 所以这就是原因。)

NSLayoutConstraint.activate([
    myView.topAnchor.constraint(equalTo: self.safeAreaLayoutGuide.topAnchor),
    myView.bottomAnchor.constraint(equalTo: self.safeAreaLayoutGuide.bottomAnchor),
    myView.leadingAnchor.constraint(equalTo: self.layoutMarginsGuide.leadingAnchor),
    myView.trailingAnchor.constraint(equalTo: self.layoutMarginsGuide.trailingAnchor),
    myView.centerXAnchor.constraint(equalTo: self.centerXAnchor)
])

I used Figma and figured out the padding of large navigation bar (for iPhoneSE) was 16, so I can set the constraint for the myView's leading anchor as我使用 Figma 并发现大型导航栏(对于 iPhoneSE)的填充为 16,因此我可以将 myView 的前导锚的约束设置为

myView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 16)

but I was wondering if there is a way to align without using a fixed constant value, 16?.但我想知道是否有一种方法可以在不使用固定常数值 16 的情况下进行对齐? I've looked up Safe Area Layout Guide and Layout Margins Guide, but I could not change the view as I wanted.我查看了 Safe Area Layout Guide 和 Layout Margins Guide ,但我无法根据需要更改视图。

I came across two ways which could potentially give this value:我遇到了两种可能赋予此价值的方法:

1. Iterating through the navigation bar hierarchy 1.遍历导航栏层次结构

var leftMargin: CGFloat = 0.0

for view in navigationController!.navigationBar.subviews
{
    if let classToFind = NSClassFromString("_UINavigationBarLargeTitleView"),
       view.isKind(of: classToFind)
    {
        leftMargin = view.layoutMargins.left
    }
}

// Set your left margin of view to align to leftMargin variable

The two unknowns here is I am not sure if this is querying a private class and if this is fine.这里的两个未知数是我不确定这是否正在查询私有 class 以及是否可以。 The second thing is, this class name could change in future iOS releases.第二件事是,这个 class 名称可能会在未来的 iOS 版本中更改。

2. systemMinimumLayoutMargins 2.systemMinimumLayoutMargins

Another thing I came across was getting the systemminimumlayoutmargins which you could do like this:我遇到的另一件事是获取systemminimumlayoutmargins ,您可以这样做:

var leftMargin = navigationController!.systemMinimumLayoutMargins.leading

// Set your left margin of view to align to leftMargin variable

This gives a value which works, however I am not sure if this was the intended usage to depict the left value of the title这给出了一个有效的值,但是我不确定这是否是描述标题左侧值的预期用途

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM