gpt4 book ai didi

ios - 布局约束不起作用和/或失败

转载 作者:行者123 更新时间:2023-11-29 02:10:59 24 4
gpt4 key购买 nike

一段时间以来,我一直在尝试让这个刷新控件居中,但我似乎无法让约束起作用。我在这里完全错过了什么吗?我阅读了一些教程,并阅读了另一个问题,通常您会向父对象添加位置约束。当我尝试将约束添加到刷新控件时,它告诉我它无法满足约束。

// Set up the refresh control and add it to the tableView
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refreshControl)

// Center the refreshControl
var centerControl: NSLayoutConstraint = NSLayoutConstraint(
item: refreshControl,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: tableView,
attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 1.0)
tableView.addConstraint(centerControl)

编辑:我试图按照建议设置 refreshControl 的大小,它仍然显示在右侧,就像它像 Any Height Any Width View Controller 一样宽 600。所以显然尝试设置大小没有任何改变

更新代码:

override func viewDidLoad() {
super.viewDidLoad()

// Set tableView delegate and dataSource
tableView.delegate = self
tableView.dataSource = self

// Set up the refresh control and add it to the tableView
self.refreshControl = UIRefreshControl(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 40))
self.refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
self.refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
self.tableView.addSubview(refreshControl)

// Center the refreshControl
let centerControl: NSLayoutConstraint = NSLayoutConstraint(
item: refreshControl,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: refreshControl.superview,
attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0)
refreshControl.superview?.addConstraint(centerControl)

代码更新#2:我已经试过了,但也没有用。我应该有一个 tableViewController 来管理 refreshControl 吗?当我添加任何这些约束(甚至是单独添加)时,刷新控件会随着表格一起被拖下。当我不添加约束时,它会保持不变。我只想得到它,这样图像和文字就不会一直偏到右边!否则它会起作用。

override func viewDidLoad() {
super.viewDidLoad()

// Set tableView delegate and dataSource
tableView.delegate = self
tableView.dataSource = self

// Set up the refresh control and add it to the tableView
refreshControl = UIRefreshControl()
refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
refreshControl.addTarget(self, action: "refresh:", forControlEvents: UIControlEvents.ValueChanged)
tableView.addSubview(refreshControl)
refreshControl.setTranslatesAutoresizingMaskIntoConstraints(false)

// Create the constraints and add them
var constX: NSLayoutConstraint =
NSLayoutConstraint(item: refreshControl,
attribute: NSLayoutAttribute.CenterX,
relatedBy: NSLayoutRelation.Equal,
toItem: tableView,
attribute: NSLayoutAttribute.CenterX,
multiplier: 1.0, constant: 0)
tableView.addConstraint(constX)

var constW: NSLayoutConstraint =
NSLayoutConstraint(item: refreshControl,
attribute: NSLayoutAttribute.Width,
relatedBy: NSLayoutRelation.Equal,
toItem: tableView,
attribute: NSLayoutAttribute.Width,
multiplier: 1.0, constant: 0)
tableView.addConstraint(constW)

var constH: NSLayoutConstraint =
NSLayoutConstraint(item: refreshControl,
attribute: NSLayoutAttribute.Height,
relatedBy: NSLayoutRelation.Equal,
toItem: nil,
attribute: NSLayoutAttribute.NotAnAttribute,
multiplier: 1.0, constant: 50)
tableView.addConstraint(constH)

}

最佳答案

尝试禁用自动调整掩码约束:

self.refreshControl.setTranslatesAutoresizingMaskIntoConstraints(false)

关于ios - 布局约束不起作用和/或失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29290733/

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