gpt4 book ai didi

ios - 如何使用 Vertical constraintsWithVisualFormat (SWIFT) 实现 UIScrollView

转载 作者:行者123 更新时间:2023-11-28 09:01:47 24 4
gpt4 key购买 nike

我需要在我的应用程序中使用 constraintsWithVisualFormat 实现 UIScrollView 可能有人会用一些代码指导我。谢谢!

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.
}
}

错误:

2015-08-04 22:26:05.693 dsds[2017:56799] Unable to simultaneously satisfy constraints. 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; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property 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. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. 2015-08-04 22:26:05.695

dsds[2017:56799] Unable to simultaneously satisfy constraints. 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; (2) find the code that added the unwanted constraint or constraints and fix it. (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property 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. The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in may also be helpful. Message from debugger: Terminated due to signal 15

救救我!!!谢谢!!!

最佳答案

阅读错误信息。它确切地告诉您问题是什么。您正在向 scrollView 添加约束,但您从未将其 translatesAutoresizingMaskIntoConstraints 设置为 false

您可能还有其他问题,但在解决该问题之前您无法继续。

关于ios - 如何使用 Vertical constraintsWithVisualFormat (SWIFT) 实现 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31821968/

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