如何在 Swift 3 中将 NSConstraints 添加到 UITextView
How to add NSConstraints to UITextView in Swift 3
因此,除了我手动添加约束的一个 View Controller 之外,我已经成功地将几乎所有项目转换为 Swift 3。
我遇到了
的错误
view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)
let views = ["textView": textView]
var constraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-70-[textView]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[textView]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
NSLayoutConstraint.activate(constraints)
所以我改成了
textView.translatesAutoresizingMaskIntoConstraints = true
view.addSubview(textView)
textView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
textView.autoresizingMask = [UIViewAutoresizing.flexibleLeftMargin, UIViewAutoresizing.flexibleRightMargin, UIViewAutoresizing.flexibleTopMargin, UIViewAutoresizing.flexibleBottomMargin]
我想做的就是在视图控制器中将 UITextView
添加到 UIView
,这样它就占据了整个高度和宽度,并在垂直和水平方向上保持在中心。我更新后的代码显然遗漏了一些明显的东西,并且不确定为什么我以前的代码拒绝 运行,因为我在 Swift 2.2 中工作。
任何人都可以回答 Swift 3 中添加约束的正确方法吗?
更新:
这是我的原始代码的错误,已转换为 Swift 3.0
2016-09-06 22:08:38.751636 APPNAME[570:65129] -[_SwiftValue
nsli_superitem]: unrecognized selector sent to instance 0x17044a0e0
2016-09-06 22:08:38.752963 APPNAME[570:65129] * Terminating app due
to uncaught exception 'NSInvalidArgumentException', reason:
'-[_SwiftValue nsli_superitem]: unrecognized selector sent to instance
0x17044a0e0'
* First throw call stack: (0x18430c1c0 0x182d4455c 0x184313278 0x184310278 0x18420a59c 0x184d42104 0x184d40948 0x184d3f79c
0x184d3f340 0x100221ec0 0x1002289ac 0x100228be8 0x18a0e5b08
0x18a19f4cc 0x18a19f3a4 0x18a19e6ec 0x18a19e138 0x18a19dcec
0x18a19dc50 0x18a0e2c78 0x1875ab40c 0x1875a00e8 0x18759ffa8
0x18751cc64 0x1875440d0 0x18a0d8348 0x1842b97dc 0x1842b740c
0x1842b789c 0x1841e6048 0x185c67198 0x18a150bd0 0x18a14b908
0x100171a04 0x1831c85b8) libc++abi.dylib: terminating with uncaught
exception of type NSException (lldb)
您的第一个代码示例在这一行中存在语法错误:
textView.addConstraints(constraints: [NSLayoutConstraint])
大概它正在尝试创建一个空数组,但是添加一个空的约束数组没有意义,所以只需删除该行。
请注意,您的第一个代码示例(删除了错误行)不符合您的要求(“占据了整个高度和宽度,并在垂直和水平方向上保持在中心位置”)。您正在指定边距,因此文本视图不会占据整个高度和宽度,并且垂直边距是不对称的,因此不会垂直居中。
第二个代码示例中的问题是您使用了错误的掩码。例如,.flexibleLeftMargin
告诉系统允许 view
的左边缘和 textView
的左边缘之间的距离发生变化,但这与您想要的相反。
这应该可以满足您的要求:
textView.frame = view.bounds
textView.autoresizingMask = [ .flexibleWidth, .flexibleWidth ]
如果您需要边距,请像这样设置框架:
var frame = view.bounds.insetBy(dx: 8, dy: 0)
frame.origin.y += 70
frame.size.height -= 78
textView.frame = frame
请注意 view
的大小必须至少为 16 x 78。
如果您想继续使用 t运行slatesAutoresizingMaskIntoConstraints=false,而不是接受的答案,我注意以下几点:
1) 当我使用视觉格式化方法生成 NSLayoutConstraints 时,我得到了你得到的错误 - 在 iOS9.3 SDK 和 运行 中编译文件的代码即使在 [=24] 的设备上也很好=] iOS10 - 使用 iOS10 SDK 构建因您的错误而崩溃
2) 如果您将其更改为手动创建 NSLayoutConstraints,例如与此类似,然后它按预期工作:
var allConstraints = [NSLayoutConstraint]()
allConstraints.append(NSLayoutConstraint(item: slideView, attribute: .left, relatedBy: .equal, toItem: slideButton, attribute: .left, multiplier: 1.0, constant: 0.0))
allConstraints.append(NSLayoutConstraint(item: slideView, attribute: .right, relatedBy: .equal, toItem: slideButton, attribute: .right, multiplier: 1.0, constant: 0.0))
allConstraints.append(NSLayoutConstraint(item: slideView, attribute: .top, relatedBy: .equal, toItem: slideButton, attribute: .top, multiplier: 1.0, constant: 0.0))
allConstraints.append(NSLayoutConstraint(item: slideView, attribute: .bottom, relatedBy: .equal, toItem: slideButton, attribute: .bottom, multiplier: 1.0, constant: 0.0))
NSLayoutConstraint.activate(allConstraints)
我不知道看起来更优雅的视觉格式有什么问题,但目前它似乎是半身像(或者 iOS 之间的视图布局代码顺序发生了变化9.3 和 10).
我在以下代码的最后一行遇到了这个错误:
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(scrollView)
contentView = UIView()
contentView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(contentView)
let viewDict = [
"contentView": contentView,
"scrollView": scrollView,
]
let vFormat = "V:|[contentView]|"
let constraints = NSLayoutConstraint.constraints(withVisualFormat: vFormat, options: [], metrics: nil, views: viewDict)
当我在最后一行设置调试器时,它显示 viewDict
类型为 [String: UIView?]
(注意这里的可选)。
当我如下更改声明时不会发生错误:
let viewDict: [String: UIView] = [
因此,除了我手动添加约束的一个 View Controller 之外,我已经成功地将几乎所有项目转换为 Swift 3。
我遇到了
的错误 view.addSubview(textView)
textView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(textView)
let views = ["textView": textView]
var constraints = NSLayoutConstraint.constraints(withVisualFormat: "V:|-70-[textView]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-8-[textView]-8-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
NSLayoutConstraint.activate(constraints)
所以我改成了
textView.translatesAutoresizingMaskIntoConstraints = true
view.addSubview(textView)
textView.center = CGPoint(x: view.bounds.midX, y: view.bounds.midY)
textView.autoresizingMask = [UIViewAutoresizing.flexibleLeftMargin, UIViewAutoresizing.flexibleRightMargin, UIViewAutoresizing.flexibleTopMargin, UIViewAutoresizing.flexibleBottomMargin]
我想做的就是在视图控制器中将 UITextView
添加到 UIView
,这样它就占据了整个高度和宽度,并在垂直和水平方向上保持在中心。我更新后的代码显然遗漏了一些明显的东西,并且不确定为什么我以前的代码拒绝 运行,因为我在 Swift 2.2 中工作。
任何人都可以回答 Swift 3 中添加约束的正确方法吗?
更新:
这是我的原始代码的错误,已转换为 Swift 3.0
2016-09-06 22:08:38.751636 APPNAME[570:65129] -[_SwiftValue nsli_superitem]: unrecognized selector sent to instance 0x17044a0e0 2016-09-06 22:08:38.752963 APPNAME[570:65129] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue nsli_superitem]: unrecognized selector sent to instance 0x17044a0e0' * First throw call stack: (0x18430c1c0 0x182d4455c 0x184313278 0x184310278 0x18420a59c 0x184d42104 0x184d40948 0x184d3f79c 0x184d3f340 0x100221ec0 0x1002289ac 0x100228be8 0x18a0e5b08 0x18a19f4cc 0x18a19f3a4 0x18a19e6ec 0x18a19e138 0x18a19dcec 0x18a19dc50 0x18a0e2c78 0x1875ab40c 0x1875a00e8 0x18759ffa8 0x18751cc64 0x1875440d0 0x18a0d8348 0x1842b97dc 0x1842b740c 0x1842b789c 0x1841e6048 0x185c67198 0x18a150bd0 0x18a14b908 0x100171a04 0x1831c85b8) libc++abi.dylib: terminating with uncaught exception of type NSException (lldb)
您的第一个代码示例在这一行中存在语法错误:
textView.addConstraints(constraints: [NSLayoutConstraint])
大概它正在尝试创建一个空数组,但是添加一个空的约束数组没有意义,所以只需删除该行。
请注意,您的第一个代码示例(删除了错误行)不符合您的要求(“占据了整个高度和宽度,并在垂直和水平方向上保持在中心位置”)。您正在指定边距,因此文本视图不会占据整个高度和宽度,并且垂直边距是不对称的,因此不会垂直居中。
第二个代码示例中的问题是您使用了错误的掩码。例如,.flexibleLeftMargin
告诉系统允许 view
的左边缘和 textView
的左边缘之间的距离发生变化,但这与您想要的相反。
这应该可以满足您的要求:
textView.frame = view.bounds
textView.autoresizingMask = [ .flexibleWidth, .flexibleWidth ]
如果您需要边距,请像这样设置框架:
var frame = view.bounds.insetBy(dx: 8, dy: 0)
frame.origin.y += 70
frame.size.height -= 78
textView.frame = frame
请注意 view
的大小必须至少为 16 x 78。
如果您想继续使用 t运行slatesAutoresizingMaskIntoConstraints=false,而不是接受的答案,我注意以下几点:
1) 当我使用视觉格式化方法生成 NSLayoutConstraints 时,我得到了你得到的错误 - 在 iOS9.3 SDK 和 运行 中编译文件的代码即使在 [=24] 的设备上也很好=] iOS10 - 使用 iOS10 SDK 构建因您的错误而崩溃
2) 如果您将其更改为手动创建 NSLayoutConstraints,例如与此类似,然后它按预期工作:
var allConstraints = [NSLayoutConstraint]()
allConstraints.append(NSLayoutConstraint(item: slideView, attribute: .left, relatedBy: .equal, toItem: slideButton, attribute: .left, multiplier: 1.0, constant: 0.0))
allConstraints.append(NSLayoutConstraint(item: slideView, attribute: .right, relatedBy: .equal, toItem: slideButton, attribute: .right, multiplier: 1.0, constant: 0.0))
allConstraints.append(NSLayoutConstraint(item: slideView, attribute: .top, relatedBy: .equal, toItem: slideButton, attribute: .top, multiplier: 1.0, constant: 0.0))
allConstraints.append(NSLayoutConstraint(item: slideView, attribute: .bottom, relatedBy: .equal, toItem: slideButton, attribute: .bottom, multiplier: 1.0, constant: 0.0))
NSLayoutConstraint.activate(allConstraints)
我不知道看起来更优雅的视觉格式有什么问题,但目前它似乎是半身像(或者 iOS 之间的视图布局代码顺序发生了变化9.3 和 10).
我在以下代码的最后一行遇到了这个错误:
scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(scrollView)
contentView = UIView()
contentView.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(contentView)
let viewDict = [
"contentView": contentView,
"scrollView": scrollView,
]
let vFormat = "V:|[contentView]|"
let constraints = NSLayoutConstraint.constraints(withVisualFormat: vFormat, options: [], metrics: nil, views: viewDict)
当我在最后一行设置调试器时,它显示 viewDict
类型为 [String: UIView?]
(注意这里的可选)。
当我如下更改声明时不会发生错误:
let viewDict: [String: UIView] = [