- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个问题...我真的有点不明白。我有一个仅创建 UITextLabel 的 UIView 类。我正在尝试将此类的实例添加到我的 Viewcontroller。这就是我的 UIViewClass 的样子:
class PostLineItem: UIView {
var labelText : String!
var labelHeader : String!
init(labelText: String , labelHeader: String) {
super.init(frame: CGRect.zeroRect)
self.labelText = labelText
self.labelHeader = labelHeader
let tlb = timeLineBlock()
//tlb.setTranslatesAutoresizingMaskIntoConstraints(false)
self.addSubview(tlb)
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: tlb, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Height, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: tlb, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: tlb, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: tlb, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0)
])
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
println("required public")
}
private func timeLineBlock() -> UIView{
let viewNew = UIView()
viewNew.setTranslatesAutoresizingMaskIntoConstraints(false)
let titleLabel = UILabel()
titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
titleLabel.font = UIFont(name: "ArialMT", size: 20)
titleLabel.textColor = UIColor.blackColor()
titleLabel.text = labelHeader
titleLabel.numberOfLines = 0
viewNew.addSubview(titleLabel)
titleLabel.backgroundColor = UIColor.redColor()
/* IF I NOT COMMENT THIS PART INSTANCE OF THIS CLASS IS NOT SHOWING UP.
viewNew.addConstraints([
NSLayoutConstraint(item: titleLabel, attribute: .Width, relatedBy: .Equal, toItem: viewNew, attribute: .Width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: titleLabel, attribute: .Left, relatedBy: .Equal, toItem: viewNew, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: titleLabel, attribute: .Top, relatedBy: .Equal, toItem: viewNew, attribute: .Top, multiplier: 1.0, constant: 0)
])
*/
viewNew.backgroundColor = UIColor.greenColor()
return viewNew
}
}
这是我的 viewController 类部分,我尝试在其中实例化 PostLineItem 实例。
let guideView = UIView()
guideView.setTranslatesAutoresizingMaskIntoConstraints(false)
guideView.backgroundColor = UIColor.blackColor()
scrollView.addSubview(guideView)
scrollView.addConstraints([
NSLayoutConstraint(item: guideView, attribute: .Top, relatedBy: .Equal, toItem: scrollView, attribute: .Top, multiplier: 1.0, constant: 50),
NSLayoutConstraint(item: guideView, attribute: .Left, relatedBy: .Equal, toItem: scrollView, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: guideView, attribute: .Width, relatedBy: .Equal, toItem: scrollView, attribute: .Width, multiplier: 1.0, constant: -30),
NSLayoutConstraint(item: guideView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant:10)
])
guideView.backgroundColor = UIColor.blackColor()
var viewFromAbove = guideView
for post in 1...70 {
let v = PostLineItem(labelText: "Tst", labelHeader: "Tst2")
scrollView.addSubview(v)
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: v, attribute: .Top, relatedBy: .Equal, toItem: viewFromAbove, attribute: .Bottom, multiplier: 1.0, constant: 15),
NSLayoutConstraint(item: v, attribute: .Left, relatedBy: .Equal, toItem: viewFromAbove, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: v, attribute: .Width, relatedBy: .Equal, toItem: viewFromAbove, attribute: .Width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: v, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 400)
])
viewFromAbove = v
}
scrollView.addConstraints([
NSLayoutConstraint(item: viewFromAbove, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: scrollView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: -10)
])
如您所见,首先我为第一个 PostLineItem 创建一个 guidview,然后添加新实例和约束.. 所以..
如果我这样做,PostLineItem 类的实例将位于同一位置并相互隐藏...我认为,NSLAyoutConstaint 无效。
但是,正如您在代码的注释部分看到的那样,如果我创建一个简单的 UILabel,则约束可以正常工作,并且所有 UILabel 实例都将位于彼此之下。
更新:在我添加所有写在答案中之后,它看起来像这样.. :(
那个红色的小矩形是我的 PostLineItem 实例.. :(
已更新 - 所有代码。
最佳答案
我认为您可能会向错误的 View 添加约束。例如,guideView
高度约束应该添加到 guideView
,而不是 scrollView
。相反,我建议您使用激活约束的新方法。您只需调用 NSLayoutConstraint
上的 activateConstraints
类方法,而不是将它们添加到 View 中:
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: guideView, attribute: .Top, relatedBy: .Equal, toItem: scrollView, attribute: .Top, multiplier: 1.0, constant: 24),
NSLayoutConstraint(item: guideView, attribute: .Left, relatedBy: .Equal, toItem: scrollView, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: guideView, attribute: .Width, relatedBy: .Equal, toItem: scrollView, attribute: .Width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: guideView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant:4)
])
和
NSLayoutConstraints.activateConstraints([
NSLayoutConstraint(item: v, attribute: .Top, relatedBy: .Equal, toItem: viewFromAbove, attribute: .Bottom, multiplier: 1.0, constant: 5),
NSLayoutConstraint(item: v, attribute: .Left, relatedBy: .Equal, toItem: viewFromAbove, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: v, attribute: .Width, relatedBy: .Equal, toItem: viewFromAbove, attribute: .Width, multiplier: 1.0, constant: 0),
])
此外,您缺少对 PostLineItem
高度的约束,例如:
NSLayoutConstraint(item: v, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant:10)
在您的 PostLineItem
初始化中,您需要为 timeLineBlock
添加约束:
init(labelText: String , labelHeader: String) {
super.init(frame: CGRect.zeroRect)
self.labelText = labelText
self.labelHeader = labelHeader
let tlb = timeLineBlock()
self.addSubview(tlb)
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: tlb, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Height, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: tlb, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: tlb, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: tlb, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0)
])
}
这是我的版本。我得到了更好的结果。看一看... 注意,我完全在 Interface Builder 中设置了我的 scrollView,并添加了一个名为 scrollView 的 socket ,它指向我的 scrollView 包含的 contentView。那应该没关系。其他一切都与您实现的一样。
class PostLineItem: UIView {
var labelText : String!
var labelHeader : String!
init(labelText: String , labelHeader: String) {
super.init(frame: CGRect.zeroRect)
self.labelText = labelText
self.labelHeader = labelHeader
let tlb = timeLineBlock()
self.addSubview(tlb)
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: tlb, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Height, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: tlb, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: tlb, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: tlb, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0)
])
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
println("required public")
}
private func timeLineBlock() -> UIView{
let viewNew = UIView()
viewNew.setTranslatesAutoresizingMaskIntoConstraints(false)
let titleLabel = UILabel()
titleLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
titleLabel.font = UIFont(name: "ArialMT", size: 20)
titleLabel.textColor = UIColor.blackColor()
titleLabel.text = labelHeader
titleLabel.numberOfLines = 0
viewNew.addSubview(titleLabel)
titleLabel.backgroundColor = UIColor.redColor()
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: titleLabel, attribute: .Width, relatedBy: .Equal, toItem: viewNew, attribute: .Width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: titleLabel, attribute: .Left, relatedBy: .Equal, toItem: viewNew, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: titleLabel, attribute: .Top, relatedBy: .Equal, toItem: viewNew, attribute: .Top, multiplier: 1.0, constant: 0)
])
viewNew.backgroundColor = UIColor.greenColor()
return viewNew
}
}
class ViewController: UIViewController {
@IBOutlet weak var scrollView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let guideView = UIView()
guideView.setTranslatesAutoresizingMaskIntoConstraints(false)
guideView.backgroundColor = UIColor.blackColor()
scrollView.addSubview(guideView)
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: guideView, attribute: .Top, relatedBy: .Equal, toItem: scrollView, attribute: .Top, multiplier: 1.0, constant: 24),
NSLayoutConstraint(item: guideView, attribute: .Left, relatedBy: .Equal, toItem: scrollView, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: guideView, attribute: .Width, relatedBy: .Equal, toItem: scrollView, attribute: .Width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: guideView, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant:4)
])
guideView.backgroundColor = UIColor.blackColor()
var viewFromAbove = guideView
for post in 1...70 {
let v = PostLineItem(labelText: "Tst", labelHeader: "Tst2")
//let v = UILabel(frame: CGRectMake(0, 0, 10, 10))
//v.text = "Tst Now with this its working, and the constraint is fine."
v.setTranslatesAutoresizingMaskIntoConstraints(false)
scrollView.addSubview(v)
NSLayoutConstraint.activateConstraints([
NSLayoutConstraint(item: v, attribute: .Top, relatedBy: .Equal, toItem: viewFromAbove, attribute: .Bottom, multiplier: 1.0, constant: 5),
NSLayoutConstraint(item: v, attribute: .Left, relatedBy: .Equal, toItem: viewFromAbove, attribute: .Left, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: v, attribute: .Width, relatedBy: .Equal, toItem: viewFromAbove, attribute: .Width, multiplier: 1.0, constant: 0),
NSLayoutConstraint(item: v, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant:30)
])
viewFromAbove = v
}
}
}
关于ios - swift NSlayoutConstraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30396437/
我正在尝试将 UIView 添加到 UITableView 的标题,然后,使用 NSLayoutConstraints 我想给它一个高度。 我已经浏览了 Apple Docs 和 WWDC 2012
我正在使用 Xcode 9 beta 5 。我务实地添加了反向操作 addConstraint(NSLayoutConstraint.constraints(withVisualFormat: "H:
我使用的是 XCode 版本 9.0 (9A235)、macOS Sierra 版本 10.12.6 (16G29)、iOS 11.0。 我需要将约束添加到数组中,然后我需要激活它。 为此我确实喜欢.
我正在通过代码构建 UICollectionViewCell 界面。一切正常,除了正确的约束。当我运行应用程序时,标题标签的右边距为零。这是我的代码 let titleLabel: UILabel =
我的代码中有两个类,一个是NameCell ,其中包含一个带有文本的简单 UILabel。第二个是NameValueCell ,它继承自该类,但还添加了属性 UIView *valueView . 需
我正在尝试设置一个面板来显示一些信息。我无法让自动布局按我的意愿工作。 这就是我想要做的事情: 我有一个 headerView、一个 UIImageView 和 5 个标签。我希望标签垂直排列,我希望
各位同事, 我开发了一个自定义键盘。键盘类型(字母、数字、特殊字符)之间的切换速度存在问题。这是由于每次重新绘制按钮时的事实。 NSLayoutConstraint我设置如下: 有一个类Keyboar
我试图对 View 内的标签 -> dateLabel 施加约束,这些是我给出的约束: let horizontalConstraint = NSLayoutConstraint(item: date
我使用编程约束将 View 定义为 iOS 屏幕上的弹出窗口。 let stopTimer = StoppageTimer(frame: CGRect.zero) 该 View 本身包含一个堆
我想将 2 个按钮移动到屏幕宽度的中心。它应该看起来像: |SecondButton(100)->| 我从第一个按钮开始。 var const = NSLayoutConstraint(item: f
这是我第一次尝试使用约束,所以请原谅这个初级问题。 我希望能够设置一系列约束,以便 subview 小于 View 宽度的 75% 或 View 宽度的 75%。因此,如果我的 subview 的宽度
我有一个在容器中的 View 在我放置在这个容器中的一些 View Controller 中,我想要全局边距 但我不知道如何将它们添加到容器中。 我的意思是我不知道如何定义 NSLayoutConst
我正在尝试通过 for 循环创建 UILabel,该循环从 NSArray 获取其 NSInteger。循环工作正常,但我看不到所有顶点一个接一个。我肯定是有约束的错误。你能帮帮我吗? 根据我发布的这
我在 UIView 中有 2 个 UILabel: [headerView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat
我下面的代码使用代码无 Storyboard 来显示对象。该项目已构建,但我看不到我试图以编程方式放置的文本字段。我不知道为什么代码没有出现。我试图不使用 Storyboard。 var enter
我在 Web View 和工具栏之间添加约束时遇到问题。我正在使用以下代码但出现错误。 NSDictionary *viewsDictionary6 = NSDictionaryOfVariableB
我有一个自定义 UITableViewCell,我正在尝试自动布局 UILabel,因为它的文本可以有任意长度,但我收到此错误: Break on objc_exception_throw to ca
我有一个包含 UIWebview 的 UIView。 UIView 具有前导和尾随约束 (20pt)。现在,如果 UIWebview 放大,我通过 UIScrollViewDelegate:scrol
我有一个程序化的 UIToolBar,它是在我的某个 ViewController 的 viewWillAppear: 方法中创建和绘制的。我有 NSLayoutConstraint 设置以将我的 t
我有一个自定义的 UITableViewCell。该单元具有三个标签。最左边的“金额”标签具有以下约束。 在单元格的右侧,我有另一个标签,“Label Dollar Amount”。它具有以下约束:
我是一名优秀的程序员,十分优秀!