gpt4 book ai didi

iOS : how to programmatically auto layout a horizontal table view?

转载 作者:行者123 更新时间:2023-11-28 09:09:49 25 4
gpt4 key购买 nike

我正在尝试编写自己的 iOS 菜单,并且我有一个水平的 UITableView 作为我的自定义 UIView(我的菜单 View )的 subview 。现在在 layoutSubviews() 中,我必须布置我的 UITableView(我正在为所有其他 subview 使用 NSLayoutConstraint.constraintsWithVisualFormat)但问题是 tableViews 的“高度”实际上是“宽度”,因为它被90 度,myTableView.transform = CGAffineTransformMakeRotation(-CGFloat(M_PI_2))。

我的 layoutSubviews() :

override func layoutSubviews() {
super.layoutSubviews()

let viewsDictionary = ["navigationTableView":navigationTableView, "settingsTableView":settingsTableView, "settingsTableViewContainerView":settingsTableViewContainerView, "settingsViewContainerView":settingsViewContainerView]
let metricsDictionary = ["navigationTableViewHeight":100.0, "settingsTableViewContainerViewHeight":70.0, "settingsViewContainerViewHeight":0.0, "viewWidth":screen.bounds.width]

navigationTableView.setTranslatesAutoresizingMaskIntoConstraints(false)
settingsTableView.setTranslatesAutoresizingMaskIntoConstraints(false)
settingsViewContainerView.setTranslatesAutoresizingMaskIntoConstraints(false)
settingsTableViewContainerView.setTranslatesAutoresizingMaskIntoConstraints(false)
// sizing constraints

// navigationTableView
let navigationTableView_constraint_H: Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[navigationTableView(viewWidth)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let navigationTableView_constraint_V: Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[navigationTableView(>=navigationTableViewHeight)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)

navigationTableView.addConstraints(navigationTableView_constraint_H)
navigationTableView.addConstraints(navigationTableView_constraint_V)

// settingsTableViewContainerView
let settingsTableViewContainerView_constraint_H: Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[settingsTableViewContainerView(viewWidth)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let settingsTableViewContainerView_constraint_V: Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[settingsTableViewContainerView(settingsTableViewContainerViewHeight)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)

settingsTableViewContainerView.addConstraints(settingsTableViewContainerView_constraint_H)
settingsTableViewContainerView.addConstraints(settingsTableViewContainerView_constraint_V)

// settingsTableView
/*let settingsTableView_constraint_H: Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[settingsTableView(settingsTableViewContainerViewHeight)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let settingsTableView_constraint_V: Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[settingsTableView(viewWidth)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)

settingsTableView.addConstraints(settingsTableView_constraint_H)
settingsTableView.addConstraints(settingsTableView_constraint_V)*/

// settingsViewContainerView
let settingsViewContainerView_constraint_H: Array = NSLayoutConstraint.constraintsWithVisualFormat("H:[settingsViewContainerView(viewWidth)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let settingsViewContainerView_constraint_V: Array = NSLayoutConstraint.constraintsWithVisualFormat("V:[settingsViewContainerView(settingsViewContainerViewHeight)]", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)

settingsViewContainerView.addConstraints(settingsViewContainerView_constraint_H)
settingsViewContainerView.addConstraints(settingsViewContainerView_constraint_V)

switch settingsTableViewPosition {
case .Top:
//Todo
break
case .Bottom:
// positioning
let settingsTableView_constraint_H: Array = NSLayoutConstraint.constraintsWithVisualFormat("H:|[settingsTableView]|", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let settingsTableView_constraint_V: Array = NSLayoutConstraint.constraintsWithVisualFormat("V:|[settingsTableView]|", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)

settingsTableViewContainerView.addConstraints(settingsTableView_constraint_H)
settingsTableViewContainerView.addConstraints(settingsTableView_constraint_V)

let view_constraint_H_navigationTableView: Array = NSLayoutConstraint.constraintsWithVisualFormat("H:|[navigationTableView]|", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let view_constraint_H_settingsTableView: Array = NSLayoutConstraint.constraintsWithVisualFormat("H:|[settingsTableViewContainerView]|", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let view_constraint_H_settingsViewContainerView: Array = NSLayoutConstraint.constraintsWithVisualFormat("H:|[settingsViewContainerView]|", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)
let view_constraint_V: Array = NSLayoutConstraint.constraintsWithVisualFormat("V:|[navigationTableView][settingsTableViewContainerView][settingsViewContainerView]|", options: NSLayoutFormatOptions(0), metrics: metricsDictionary, views: viewsDictionary)

self.addConstraints(view_constraint_H_settingsTableView)
self.addConstraints(view_constraint_H_navigationTableView)
self.addConstraints(view_constraint_H_settingsViewContainerView)
self.addConstraints(view_constraint_V)
break
}
}

最佳答案

我建议您使用 UICollectionView 而不是 UITableView。这将完全解决您的问题。根据我的经验,如果您“将 tableview 横向旋转 90 度”,您将在功能中遇到很多错误。

我不确定,但您可以针对您的问题尝试将 TableView 添加到容器中。将 tableview 的左、上、下、右约束连接到容器宽度 .constant = 0。然后像往常一样为您的容器设置约束。如果它无助于将 tableview.height 约束设置为 container.width 约束并将 tableview.width 设置为 container.height。

关于iOS : how to programmatically auto layout a horizontal table view?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29590182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com