如何向自定义 UITableViewCell 添加约束
How to add constraints to a Custom UITableViewCell
我已经为我的 table 视图创建了一个自定义单元格,它工作正常,只是我的图像视图无法正确对齐。
有没有办法向自定义单元格视图添加约束?以编程方式或其他方式?
这是我的单元格在情节提要中的样子 - 这是我希望在 运行 时间实现的:
但是当我演示应用程序时,会发生以下情况:
这是故事板中的另一张图片:
视图控制器/w TableView
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
println(questions)
println(finalResults)
let theWidth = view.frame.size.width
let theHeight = view.frame.size.height
tableView.frame = CGRectMake(0,0, theHeight, theWidth)
}
单元格视图控制器
override func awakeFromNib() {
let theWidth = UIScreen.mainScreen().bounds.width
contentView.frame = CGRectMake(0,0, theWidth, 64)
answerImage.center = CGPointMake(115, 15)
}
假设您从一个视图控制器开始,将一个 table 和一个单元格拖到 table...
在 ViewController 中:UIViewController...{
@IBOutlet weak var resultsTable: UITableView! // connect to tableView
override func viewDidLoad() {
super.viewDidLoad()
let theWidth = view.frame.size.width
let theHeight = view.frame.size.height
resultsTable.frame = CGRectMake(0,0, theWidth, theHeight)
}
}
在 UITableViewCell 文件中:
@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
let theWidth = UIScreen.mainScreen().bounds.width
contentView.frame = CGRectMake(0,0 theWidth, 64)
imageView.center = CGPointMake(115, 15) //mess around with these #
}
您只需正确设置约束即可。的确,即使Add Missing Constraints
和Reset to Suggested Constraints
很方便,但有时他们并不知道你想要什么,因此结果并不总是你所期望的。
您可能需要执行以下操作。
对于你的问题,标签将其设置在其容器的中心 Y 并将其引导 space 到超级视图。像这样:
那么对于你的图像,你也想将它设置在容器的 Y 中心并设置一个比例 1:1。像这样:
请注意,如果您不希望图像放大(如果您在某些设备上设置了更大的单元格),您可能还需要检查 width
和 height
).
你现在应该有类似的东西了:
结果:
如果有帮助请告诉我。
PS : 我没有你的图片所以我只是在 uiimageview
上设置了背景颜色
我已经为我的 table 视图创建了一个自定义单元格,它工作正常,只是我的图像视图无法正确对齐。
有没有办法向自定义单元格视图添加约束?以编程方式或其他方式?
这是我的单元格在情节提要中的样子 - 这是我希望在 运行 时间实现的:
但是当我演示应用程序时,会发生以下情况:
这是故事板中的另一张图片:
视图控制器/w TableView
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
println(questions)
println(finalResults)
let theWidth = view.frame.size.width
let theHeight = view.frame.size.height
tableView.frame = CGRectMake(0,0, theHeight, theWidth)
}
单元格视图控制器
override func awakeFromNib() {
let theWidth = UIScreen.mainScreen().bounds.width
contentView.frame = CGRectMake(0,0, theWidth, 64)
answerImage.center = CGPointMake(115, 15)
}
假设您从一个视图控制器开始,将一个 table 和一个单元格拖到 table...
在 ViewController 中:UIViewController...{
@IBOutlet weak var resultsTable: UITableView! // connect to tableView
override func viewDidLoad() {
super.viewDidLoad()
let theWidth = view.frame.size.width
let theHeight = view.frame.size.height
resultsTable.frame = CGRectMake(0,0, theWidth, theHeight)
}
}
在 UITableViewCell 文件中:
@IBOutlet weak var imageView: UIImageView!
override func awakeFromNib() {
let theWidth = UIScreen.mainScreen().bounds.width
contentView.frame = CGRectMake(0,0 theWidth, 64)
imageView.center = CGPointMake(115, 15) //mess around with these #
}
您只需正确设置约束即可。的确,即使Add Missing Constraints
和Reset to Suggested Constraints
很方便,但有时他们并不知道你想要什么,因此结果并不总是你所期望的。
您可能需要执行以下操作。
对于你的问题,标签将其设置在其容器的中心 Y 并将其引导 space 到超级视图。像这样:
那么对于你的图像,你也想将它设置在容器的 Y 中心并设置一个比例 1:1。像这样:
请注意,如果您不希望图像放大(如果您在某些设备上设置了更大的单元格),您可能还需要检查 width
和 height
).
你现在应该有类似的东西了:
结果:
如果有帮助请告诉我。
PS : 我没有你的图片所以我只是在 uiimageview
上设置了背景颜色