我正在尝试构建拖放操作,如果我从堆栈视图中拖动视图并将其拖放到某处并使用“removeArrangedSubview”删除视图,则会更改拖动的项目大小并使其更大。我正在使用这段代码来调整已删除项目的大小。
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)
答案 0 :(得分:1)
您正在设置要移动的视图的框架,但是当您将其放置在堆栈视图中时,堆栈视图将根据堆栈视图的约束以及您所在视图的内在内容大小重置它加入它。当你不打电话给removeArrangedSubview
时,它没有这样做是因为它没有触发自动布局通行证。一般情况下,当您使用自动布局时,您不应设置视图框架,而应更新约束intrinsicContentSize
并请求自动布局更新。
您应该使用堆栈视图的distribution
属性,放置的约束条件以及要添加到其中的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)
}
}
上面的视图控制器和UIView
子类与您正在寻找的内容类似,但是无法从您的问题中判断出来。请注意,将堆栈视图固定到其超级视图的边缘(如stackView
)会导致它扩展子视图以填充所有可用空间,同时允许堆栈视图根据其子视图的内在大小动态调整大小(如otherStackView
)没有。
如果您不想将视图添加到另一个堆栈视图但希望它们保留其框架,则应删除视图对其进行的任何约束,然后设置其{{1} }属性到translatesAutoresizingMaskIntoConstraints
。例如:
true