Lan*_*ria 18 xcode constraints uiview ios swift
在IB内部,我有一个带有segmentedControl和2个containerViews的viewController.在每个containerView中我都有一个tableView.从每个tableView我推送一个detailView.我的自动布局是在IB中.
一旦我选择一个段,我看到相应的tableView,我选择一个单元格,并在场景中推送正确的detailView.
问题是一旦在segmentedControl上推送detailView仍然可见.我决定隐藏有效的segmentedControl,但那里有一个很大的空白空间.我尝试以编程方式增加detailView视图的大小,但它没有扩展.从我读到它是因为我在故事板中做了初始约束.
如何以编程方式获取detailView的视图以进行扩展?
码:
DetailViewController: UIViewController{
override func viewDidLoad() {
super.viewDidLoad()
//this isn't increasing the view's size
self.view.frame.size.height += 20.0
//doesn't work. I get an error on the last argument
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)
//doesn't work. I get an error on the last argument
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.size.height += 20.0)
}
}
Run Code Online (Sandbox Code Playgroud)
错误:
//Error for the last argument- height: *self.view.frame.height += 20.0*
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)
Run Code Online (Sandbox Code Playgroud)
变异运算符的左侧不可变:'height'是get-only属性
//Error for the last argument- *self.view.frame.size.height += 20.0*:
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.size.height += 20.0)
Run Code Online (Sandbox Code Playgroud)
无法使用类型为'(x:Int,y:Int,width:CGFloat,height:())的参数列表调用类型'CGRect'的初始值设定项
Coo*_*uin 34
您需要为详细视图的高度约束创建一个出口,然后您可以通过编程方式调整高度myHeightConstraint.constant += extraHeight
,其中extraHeight
一个数字表示您希望详细视图的高度.您也可能需要在self.view.layoutIfNeeded()
之后致电.
要从约束创建出口,请控制从情节提要编辑器中的约束(就像您对UIView的出口一样)拖动到代码中.
你是对的 - 因为你正在使用自动布局和约束,所以也需要使用约束进行调整.设置原始帧可能会导致意外行为.如果您有任何其他问题或困难,请告诉我.
rma*_*ddy 13
在CGRect
初始化程序中你不应该使用+=
,只是+
.
更改:
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height += 20.0)
Run Code Online (Sandbox Code Playgroud)
至:
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height + 20.0)
Run Code Online (Sandbox Code Playgroud)
您不应该尝试更改self.view.frame.height
初始化程序内部的值.
小智 7
斯威夫特 4.2
我以编程方式为您创建一个示例:
var flowHeightConstraint: NSLayoutConstraint?
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
view.addSubview(button)
button.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
button.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
button.widthAnchor.constraint(equalToConstant: 100).isActive = true
flowHeightConstraint = button.heightAnchor.constraint(equalToConstant: 30)
flowHeightConstraint?.isActive = true
}
@objc func animateButtonTapped() {
UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: .curveEaseOut, animations: {
self.flowHeightConstraint?.constant = 100
self.view.layoutIfNeeded()
}, completion: nil)
}
lazy var button: UIButton = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = .green
button.addTarget(self, action: #selector(animateButtonTapped), for: .touchUpInside)
return button
}()
Run Code Online (Sandbox Code Playgroud)
这行代码创建了一个按钮,并通过动画更改按钮的高度。
希望这有帮助:)
对我来说,程序化解决方案不需要我为我可能想要修改的每个高度约束创建一个出口。这是一个仅适用于我的代码解决方案。无论原始约束是在情节提要上创建还是以编程方式创建,它都应该起作用。如果需要,它还提供了一个为更改设置动画的选项——尽管我不确定如果视图嵌套在复杂的层次结构中这是否有效。
extension UIView {
func setHeight(_ h:CGFloat, animateTime:TimeInterval?=nil) {
if let c = self.constraints.first(where: { $0.firstAttribute == .height && $0.relation == .equal }) {
c.constant = CGFloat(h)
if let animateTime = animateTime {
UIView.animate(withDuration: animateTime, animations:{
self.superview?.layoutIfNeeded()
})
}
else {
self.superview?.layoutIfNeeded()
}
}
}
}
Run Code Online (Sandbox Code Playgroud)