【问题标题】:Why does the UITableView goes below the TabBar?为什么 UITableView 会低于 TabBar?
【发布时间】:2019-07-25 05:37:02
【问题描述】:

这是它目前的样子:

Image here

这是我当前的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = UIColor(red: 4/255, green: 4/255, blue: 4/255, alpha: 1.0)
    self.navigationController?.navigationBar.barStyle = .black
    self.navigationController?.navigationBar.tintColor = UIColor.white
    self.navigationItem.title = "Test"
    self.navigationController?.navigationBar.prefersLargeTitles = true

    // Get main screen bounds
    let screenSize: CGRect = UIScreen.main.bounds
    let screenWidth = screenSize.width
    let screenHeight = screenSize.height


    myView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 150)
    myView.backgroundColor = .red
    self.view.addSubview(myView)



    myTableView.frame = CGRect(x: 0, y: myView.frame.size.height, width: screenWidth, height: screenHeight-myView.frame.size.height-(navigationController?.navigationBar.frame.size.height)!-(tabBarController?.tabBar.frame.size.height)!)
    print("SCREEN: \(screenHeight)")
    print("TABLEVIEW: \(myTableView.frame.size.height)")
    myTableView.dataSource = self
    myTableView.delegate = self
    myTableView.backgroundColor = .blue
    myTableView.layer.borderWidth = 3

    myTableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

    self.view.addSubview(myTableView)
}

看起来我的编码正确。同样在情节提要的属性检查器中,我没有选中Extend Edges: Under Bottom Bar。有什么想法吗?

【问题讨论】:

  • 使用frames 添加视图的大小是一种非常糟糕的方法。请改用NSLayoutConstraints

标签: ios swift xcode uitableview uiview


【解决方案1】:

在这里猜测,但您可能将自动调整大小的掩码转换为约束。结果破坏了您的布局。试试:

myView.autoresizingMask = []
myTableView.autoresizingMask = []

// or alternatively

myView.translatesAutoresizingMaskIntoConstraints = false
myTableView.translatesAutoresizingMaskIntoConstraints = false

但是设置是否正确并不重要,因为您是手动计算实际布局。尝试使用自动布局:

    myView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(myView)

    myView.topAnchor.constraint(equalTo: self.view.topAnchor).isActive = true
    myView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    myView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    myView.heightAnchor.constraint(equalToConstant: 150).isActive = true

    myTableView.translatesAutoresizingMaskIntoConstraints = false
    self.view.addSubview(myTableView)

    myTableView.topAnchor.constraint(equalTo: myView.bottomAnchor).isActive = true // making myTableView to lie just below myView
    myTableView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true
    myTableView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true
    myTableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多