gpt4 book ai didi

swift - 从 UIStackView 中移除 UIView 会改变它的大小

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

我正在尝试构建拖放操作,如果我将 View 从堆栈 View 拖放到某个位置并使用“removeArrangedSubview”删除 View ,它会更改拖动项目的大小并使其变大。我正在使用这段代码来调整掉落元素的大小。

sender.view!.frame = CGRectMake(sender.view!.frame.origin.x, sender.view!.frame.origin.y, sender.view!.frame.width * 0.5, sender.view!.frame.height * 0.5)

这是用于比较的图像。 enter image description here

最佳答案

您正在设置要移动的 View 的框架,但是当您将它放在堆栈 View 中时,堆栈 View 将根据堆栈 View 的约束和您要添加到其中的 View 的固有内容大小重置它。当您不调用 removeArrangedSubview 时它不这样做的原因是因为它没有触发自动布局传递。通常,当您使用自动布局时,您不应设置 View 框架,而应更新约束 intrinsicContentSize 并请求自动布局更新。

您应该尝试使用堆栈 View 的 distribution 属性、对其施加的约束以及要添加到的 View 的 intrinsicContentSize直到你得到你想要的结果。

例如:

class ViewController: UIViewController
{
let stackView = UIStackView()
let otherStackView = UIStackView()

override func viewDidLoad()
{
self.view.backgroundColor = UIColor.whiteColor()

stackView.alignment = .Center
stackView.axis = .Horizontal
stackView.spacing = 10.0
stackView.distribution = .FillEqually
stackView.translatesAutoresizingMaskIntoConstraints = false

otherStackView.alignment = .Center
otherStackView.axis = .Horizontal
otherStackView.spacing = 10.0
otherStackView.distribution = .FillEqually
otherStackView.translatesAutoresizingMaskIntoConstraints = false

self.view.addSubview(stackView)
self.view.addSubview(otherStackView)

stackView.bottomAnchor.constraintEqualToAnchor(self.view.bottomAnchor).active = true
stackView.leadingAnchor.constraintEqualToAnchor(self.view.leadingAnchor).active = true
stackView.trailingAnchor.constraintEqualToAnchor(self.view.trailingAnchor).active = true
stackView.heightAnchor.constraintEqualToConstant(150).active = true

otherStackView.topAnchor.constraintEqualToAnchor(self.view.topAnchor).active = true
otherStackView.widthAnchor.constraintGreaterThanOrEqualToConstant(50).active = true
otherStackView.centerXAnchor.constraintEqualToAnchor(self.view.centerXAnchor).active = true
otherStackView.heightAnchor.constraintEqualToConstant(150).active = true

self.addSubviews()
}

func addSubviews()
{
for _ in 0 ..< 5
{
let view = View(frame: CGRect(x: 0.0, y: 0.0, width: 100, height: 100))
view.userInteractionEnabled = true
let panGesture = UIPanGestureRecognizer(target: self, action: #selector(self.panHandler(_:)))
view.addGestureRecognizer(panGesture)
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.redColor()
self.stackView.addArrangedSubview(view)
}
}

func panHandler(sender: UIPanGestureRecognizer)
{
guard let view = sender.view else{ return }
switch sender.state {
case .Began:
self.stackView.removeArrangedSubview(view)
self.view.addSubview(view)
let location = sender.locationInView(self.view)
view.center = location
case .Changed:
view.center.x += sender.translationInView(self.view).x
view.center.y += sender.translationInView(self.view).y
case .Ended:
if CGRectContainsPoint(self.otherStackView.frame, view.center)
{
self.otherStackView.addArrangedSubview(view)
self.otherStackView.layoutIfNeeded()
}
else
{
self.stackView.addArrangedSubview(view)
self.otherStackView.layoutIfNeeded()
}
default:
self.stackView.addArrangedSubview(view)
self.otherStackView.layoutIfNeeded()
}
sender.setTranslation(CGPointZero, inView: self.view)
}
}

class View: UIView
{
override func intrinsicContentSize() -> CGSize
{
return CGSize(width: 100, height: 100)
}
}

上面的 View Controller 和 UIView 子类做的事情与您正在寻找的类似,但无法从您的问题中分辨出来。请注意,将堆栈 View 固定到其 super View (如 stackView)的边缘会导致它拉伸(stretch) subview 以填充所有可用空间,同时允许堆栈 View 根据其固有大小动态调整大小 subview (如 otherStackView)没有。

更新

如果您不想将 View 添加到另一个堆栈 View ,但希望它们保留它们的框架,您应该删除 View 对它们的任何约束,然后将它们的 translatesAutoresizingMaskIntoConstraints 属性设置为。例如:

func panHandler(sender: UIPanGestureRecognizer)
{
guard let view = sender.view else{ return }
switch sender.state {
case .Began:
self.stackView.removeArrangedSubview(view)
self.view.addSubview(view)
let location = sender.locationInView(self.view)
view.center = location
case .Changed:
view.center.x += sender.translationInView(self.view).x
view.center.y += sender.translationInView(self.view).y
default:
view.removeConstraints(view.constraints)
view.translatesAutoresizingMaskIntoConstraints = true
// You can now set the view's frame or position however you want
view.center.x += sender.translationInView(self.view).x
view.center.y += sender.translationInView(self.view).y

}
sender.setTranslation(CGPointZero, inView: self.view)
}

关于swift - 从 UIStackView 中移除 UIView 会改变它的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39164897/

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