[英]How to implement a UIScrollView using Vertical constraintsWithVisualFormat (SWIFT)
I need to implement UIScrollView in my app using constraintsWithVisualFormat possible that someone would guide me with some code . 我需要使用ConstraintsWithVisualFormat在我的应用程序中实现UIScrollView,可能有人会指导我一些代码。 Thank You!
谢谢!
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate{
var scrollView: UIScrollView!
var containerView = UIView()
//FUNCION PARA PERMITIR COLORES HEXÁDECIMALES
func uicolorFromHex(rgbValue:UInt32)->UIColor{
let red = CGFloat((rgbValue & 0xFF0000) >> 16)/256.0;
let green = CGFloat((rgbValue & 0xFF00) >> 8)/256.0;
let blue = CGFloat(rgbValue & 0xFF)/256.0;
return UIColor(red:red, green:green, blue:blue, alpha:1.0);
}
//***
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView = UIScrollView()
self.scrollView.delegate = self
containerView = UIView()
containerView.backgroundColor = uicolorFromHex(0x0099FF);
containerView.setTranslatesAutoresizingMaskIntoConstraints(false);
scrollView.addSubview(containerView)
view.addSubview(scrollView)
//*** DICCIONARIO
let viewsDictionary = ["scrollView":scrollView
,"containerView":containerView]
let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: viewsDictionary)
let view_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView]-0-|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: viewsDictionary)
view.addConstraints(view_constraint_H as [AnyObject])
view.addConstraints(view_constraint_V as [AnyObject])
let view_constraintContainer_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[containerView]-0-|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: viewsDictionary)
let view_constraintContainer_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[containerView]-0-|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: viewsDictionary)
scrollView.addConstraints(view_constraintContainer_H as [AnyObject])
scrollView.addConstraints(view_constraintContainer_V as [AnyObject])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
error: 错误:
2015-08-04 22:26:05.693 dsds[2017:56799] Unable to simultaneously satisfy constraints.
2015-08-04 22:26:05.693 dsds [2017:56799]无法同时满足约束。 Probably at least one of the constraints in the following list is one you don't want.
以下列表中至少有一个约束是您不想要的约束。 Try this: (1) look at each constraint and try to figure out which you don't expect;
尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束; (2) find the code that added the unwanted constraint or constraints and fix it.
(2)查找添加了一个或多个不必要约束的代码并进行修复。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) (
(注意:如果看到的是您不了解的NSAutoresizingMaskLayoutConstraints,请参阅有关UIView属性的文档translatesAutoresizingMaskIntoConstraints)(
"<NSLayoutConstraint:0x7a7cfc00 H:|-(0)-[UIScrollView:0x7c188200] (Names: '|':UIView:0x7a8499d0 )>", "<NSLayoutConstraint:0x7a7cfe50 H:[UIScrollView:0x7c188200]-(0)-| (Names: '|':UIView:0x7a8499d0 )>", "<NSAutoresizingMaskLayoutConstraint:0x7a7df310 h=--& v=--& H:[UIScrollView:0x7c188200(0)]>", "<NSLayoutConstraint:0x7a7d04e0 'UIView-Encapsulated-Layout-Width' H:[UIView:0x7a8499d0(320)]>" )
Will attempt to recover by breaking constraint
将尝试通过打破约束来恢复
<NSLayoutConstraint:0x7a7cfe50 H:[UIScrollView:0x7c188200]-(0)-| (Names: '|':UIView:0x7a8499d0 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点。 The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。 2015-08-04 22:26:05.695
2015-08-04 22:26:05.695
dsds[2017:56799] Unable to simultaneously satisfy constraints.
dsds [2017:56799]无法同时满足约束。 Probably at least one of the constraints in the following list is one you don't want.
以下列表中至少有一个约束是您不想要的约束。 Try this: (1) look at each constraint and try to figure out which you don't expect;
尝试以下操作:(1)查看每个约束,并尝试找出不期望的约束; (2) find the code that added the unwanted constraint or constraints and fix it.
(2)查找添加了一个或多个不必要约束的代码并进行修复。 (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) (
(注意:如果看到的是您不了解的NSAutoresizingMaskLayoutConstraints,请参阅有关UIView属性的文档translatesAutoresizingMaskIntoConstraints)(
"<NSLayoutConstraint:0x7a7cfa00 V:|-(0)-[UIScrollView:0x7c188200] (Names: '|':UIView:0x7a8499d0 )>", "<NSLayoutConstraint:0x7a7d0160 V:[UIScrollView:0x7c188200]-(0)-| (Names: '|':UIView:0x7a8499d0 )>", "<NSAutoresizingMaskLayoutConstraint:0x7a7df370 h=--& v=--& V:[UIScrollView:0x7c188200(0)]>", "<NSLayoutConstraint:0x7a7df920 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7a8499d0(568)]>" )
Will attempt to recover by breaking constraint
将尝试通过打破约束来恢复
<NSLayoutConstraint:0x7a7d0160 V:[UIScrollView:0x7c188200]-(0)-| (Names: '|':UIView:0x7a8499d0 )>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
在UIViewAlertForUnsatisfiableConstraints上创建一个符号断点,以在调试器中捕获该断点。 The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful.
列出的UIView的UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。 Message from debugger: Terminated due to signal 15
来自调试器的消息:由于信号15而终止
help me!!!! 帮我!!!! thanks!!!
谢谢!!!
Read the error message. 阅读错误消息。 It tells you exactly what the issue is.
它准确地告诉您问题所在。 You are adding constraints to your
scrollView
but you never set its translatesAutoresizingMaskIntoConstraints
to false
. 您正在向您的
scrollView
添加约束,但是您从未将其translatesAutoresizingMaskIntoConstraints
设置为false
。
You probably have other problems too, but you cannot proceed until you fix that one. 您可能也有其他问题,但是只有解决该问题后才能继续。
You forgot 你忘了
scrollView.setTranslatesAutoresizingMaskIntoConstraints(false);
Remember the docs clearly say all programmatically created views must set this. 请记住,文档明确指出所有以编程方式创建的视图都必须对此进行设置。
On another note...why are you putting yourself through this pain for something so clearly suited for StoryBoard's visual AutoLayout?!? 另一方面,...为什么要让自己承受如此明显适合StoryBoard视觉自动布局的痛苦?!
As Matt said you have other problems...such as the fact that UIScrollViews do not behave like other containers. 正如Matt所说的,您还有其他问题……例如UIScrollViews的行为不像其他容器。 Constraining a UIScrollView's subview to its edges is pointless.
将UIScrollView的子视图约束到其边缘是没有意义的。 A UIScrollView should have a
contentSize
set. UIScrollView应该具有
contentSize
设置。
Fixed up version: 固定版本:
import UIKit
extension UIColor {
static func fromUInt(rgbValue: UInt32) -> UIColor{
let red = CGFloat((rgbValue & 0xFF0000) >> 16) / 256.0
let green = CGFloat((rgbValue & 0xFF00) >> 8) / 256.0
let blue = CGFloat(rgbValue & 0xFF) / 256.0
return UIColor(red: red, green: green, blue: blue, alpha: 1.0)
}
}
class ViewController: UIViewController, UIScrollViewDelegate {
var scrollView = UIScrollView()
var containerView = UIView()
override func viewDidLoad() {
super.viewDidLoad()
scrollView.delegate = self
containerView.backgroundColor = UIColor.fromUInt(0x0099FF)
containerView.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addSubview(containerView)
view.addSubview(scrollView)
let viewsDictionary = ["scrollView": scrollView]
let view_constraint_H = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[scrollView]-0-|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: viewsDictionary)
let view_constraint_V = NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[scrollView]-0-|",
options: NSLayoutFormatOptions(0),
metrics: nil,
views: viewsDictionary)
view.addConstraints(view_constraint_H)
view.addConstraints(view_constraint_V)
containerView.frame = CGRectMake(0, 0, 2000, 2000)
scrollView.contentSize = containerView.frame.size
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.