简体   繁体   English

是否有一种 Swift 方法可以根据 cellLayoutMarginsFollowReadableWidth 缩进导航栏的大标题?

[英]Is there a Swift method for indenting the large title of a navigation bar in line with cellLayoutMarginsFollowReadableWidth?

Currently, on iPad, my UINavigationBar large title looks strange because it does not indent to where my UITableView cells are.目前,在 iPad 上,我的 UINavigationBar 大标题看起来很奇怪,因为它没有缩进到我的 UITableView 单元格所在的位置。 This is because my table view and cells follow readable width.这是因为我的表格视图和单元格遵循可读宽度。 Is there a way of indenting this title or is the only piece of advice to align the cells with the title (make them not follow readable width).有没有办法缩进这个标题,或者是将单元格与标题对齐的唯一建议(使它们不遵循可读宽度)。 I would really appreciate anyone who took the time to respond.我真的很感激任何花时间做出回应的人。

When you first load your view controller, you should retrieve the minX coordinate of the current readableContentGuide and set that value to the left layoutMargin value.当您第一次加载视图控制器时,您应该检索当前readableContentGuideminX坐标并将该值设置为 left layoutMargin值。 As so:如此:

let leftMargin = self.view.readableContentGuide.layoutFrame.minX    
self.navigationController?.navigationBar.layoutMargins.left = leftMargin

Then, you'll also want to make sure your layout adjusts dynamically so that if the user rotates their screen (or on an iPad, is using a variable window size) the title will remain aligned:然后,您还需要确保您的布局动态调整,以便如果用户旋转他们的屏幕(或在 iPad 上,使用可变窗口大小)标题将保持对齐:

override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransition(to: size, with: coordinator)

    coordinator.animate(alongsideTransition: { _ in
        let leftMargin = view.readableContentGuide.layoutFrame.minX    
        navigationController?.navigationBar.layoutMargins.left = leftMargin
    }, completion: nil)
}

The answer of @swillsea did not work correctly for me: on rotation of the device the left margin of the large title seems to have a wrong initial position during the rotation animation. @swillsea 的答案对我来说不起作用:在旋转设备时,大标题的左边距在旋转动画期间似乎有错误的初始位置。 One can see this in the simulator with the options for slow animations enabled.可以在模拟器中看到这一点,并启用慢速动画选项。

The reason for this is that the screen is laid out in the new dimension using the old left margin.这样做的原因是使用旧的左边距在新维度中布置屏幕。 During the rotation animation the margin is animated into the correct final size.在旋转动画期间,边距动画为正确的最终尺寸。

Instead of updating the left margin when the size changes I simply update as part of the normal layout chain:当大小改变时,我没有更新左边距,而是作为正常布局链的一部分进行更新:

override func viewWillLayoutSubviews() {
    let leftMargin = self.view.readableContentGuide.layoutFrame.minX
    self.navigationController?.navigationBar.layoutMargins.left = leftMargin
}

The effect for me is now, that the left margin of the title is now equal to that of my readable content in all phases of the rotation animation.对我来说,现在的效果是,在旋转动画的所有阶段,标题的左边距现在等于我的可读内容的左边距。

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

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