繁体   English   中英

iOS TableView第一个单元格图像应从状态栏开始

[英]IOS TableView first cell Image should start from status bar

创建的表视图中的第一个单元格包含一个图像,我希望此图像与状态栏重叠,如下图所示。 如何相应地创建NavigationBar?

在此处输入图片说明

尝试以下代码行对我不起作用。

tableView.contentInset = UIEdgeInsets(top: -UIApplication.shared.statusBarFrame.size.height, left: 0, bottom: 0, right: 0)

还可以在安全区域和超级视野中玩耍,但是没有运气

更新:

现在已将s​​uperspace的顶部空间添加到tableview的superview -90约束中,该图像在状态栏中可见,想知道这是正确的方法还是其他方法

在此处输入图片说明

发现由于导航控制器和导航栏在顶部有空白空间,直到我将顶部空间设置为超级视图,因为-90以下是图像,它才起作用

在此处输入图片说明

在StoryBoard中,我可以检查导航项占用的顶部空白空间

在此处输入图片说明

首先,调整约束与下图相同:

表约束

然后使用三个按钮创建“俯视图/导航”视图,该视图应位于桌面视图上方且背景色clear 与上图相同。 并且该顶视图的top constraint应与safe areatop aligned

将以下方法放入您的控制器:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.setNavigationBarHidden(true, animated: true)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    self.navigationController?.setNavigationBarHidden(false, animated: true)
}

现在,当您运行时,它将完全看起来像您想要的。

表第一个单元格应覆盖导航栏

您是否尝试过将最大的约束不附加在安全区域上,而是附加在监督上?

您也可以尝试以下方法:

 self.contentInsetAdjustmentBehavior = .never 

将tableview 顶部约束0设置为superview 并在viewDidLoad中使用以下代码:

    tableView.contentInsetAdjustmentBehavior = .never

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
    self.navigationController?.navigationBar.shadowImage = UIImage()
    self.navigationController?.navigationBar.isTranslucent = true
    self.view.backgroundColor = UIColor.clear

将tableview.top的顶部约束添加到superview.top

将tableview的顶部约束设为0以进行超级视图,并使导航栏透明。 如果需要,请使用我制作的扩展名以使导航栏透明。

extension UINavigationController {

    func transparent()  {
        self.navigationBar.setBackgroundImage(UIImage(), for: .default)
        self.navigationBar.shadowImage = UIImage()
        self.navigationBar.isTranslucent = true // set bartintColor if isTranslucent is false
        self.view.backgroundColor = UIColor.clear
    }

    func nonTransparent() {
        self.view.backgroundColor = UIColor.black.withAlphaComponent(0.75)
        self.navigationBar.isTranslucent = false
        self.navigationBar.barTintColor = UIColor.white
    }

}

您可以“模拟”导航栏并实现您的目标。

  1. 将tableView约束到superview.top 0

  2. 设置tableview.contentInsetsAdjustmentsBehavior = .never

  3. 在viewWillAppear上调用self.navigationController?.setNavigationBarHidden(true, animated: animated)

  4. 将后退按钮和其他项目放置在表格视图上方的导航栏中。 可以使用tableView的超级视图,但不要忘记将它们移到tableView上方。 为他们使用最安全的区域,以便像放置在navigationBar中一样锁定。

注意:此操作将禁用向后导航的边缘平移手势。

为了创建该布局:

  1. 您必须将tableView固定到view.TopAnchor

  2. 将contentInsetAdjustmentBehavior设置为never tableView.contentInsetAdjustmentBehavior = .never

  3. 将单元格内的imageView的topAnchor设置为contentView.topAnchor
  4. 设置navigationController的背景图像和阴影图像: navigationBar.setBackgroundImage(UIImage(), for: .default) navigationBar.shadowImage = UIImage()

    让我知道是否一切都好。 它必须工作

暂无
暂无

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

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