- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我以编程方式向特定 View 添加了 4 个 subview (label1、label2、label3、label4)和约束(label2 顶部取决于 label1 底部等)。现在我使用 insertSubiewAtIndex 方法在索引 1(label5) 处插入另一个 subview 。现在如何刷新 UI,因为约束是动态的,意味着 label5 应该占据 label1 位置,label1 应该占据 label2 位置,依此类推。
这是我的代码
override func viewDidLoad() {
super.viewDidLoad()
let label1 = UILabel()
label1.translatesAutoresizingMaskIntoConstraints = false
label1.text = "label1"
label1.numberOfLines = 0
label1.tag = 1
self.containerView.addSubview(label1)
NSLayoutConstraint(item: label1, attribute: .top, relatedBy: .equal, toItem: self.containerView, attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true
NSLayoutConstraint(item: label1, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: label1, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true
let label2 = UILabel()
label2.translatesAutoresizingMaskIntoConstraints = false
label2.text = "label2"
label2.numberOfLines = 0
label2.tag = 1
self.containerView.addSubview(label2)
NSLayoutConstraint(item: label2, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[self.containerView.subviews.count - 2], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true
NSLayoutConstraint(item: label2, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: label2, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true
let label3 = UILabel()
label3.translatesAutoresizingMaskIntoConstraints = false
label3.text = "label3"
label3.numberOfLines = 0
label3.tag = 1
self.containerView.addSubview(label3)
NSLayoutConstraint(item: label3, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[self.containerView.subviews.count - 2], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true
NSLayoutConstraint(item: label3, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: label3, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true
let label4 = UILabel()
label4.translatesAutoresizingMaskIntoConstraints = false
label4.text = "label4"
label4.numberOfLines = 0
label4.tag = 1
self.containerView.addSubview(label4)
NSLayoutConstraint(item: label4, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[self.containerView.subviews.count - 2], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true
NSLayoutConstraint(item: label4, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: label4, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true
let label5 = UILabel()
label5.translatesAutoresizingMaskIntoConstraints = false
label5.text = "label5"
label5.numberOfLines = 0
label5.tag = 1
self.containerView.addSubview(label5)
NSLayoutConstraint(item: label5, attribute: .top, relatedBy: .equal, toItem: self.containerView.subviews[0], attribute: .top, multiplier: 1.0, constant: 16.0).isActive = true
NSLayoutConstraint(item: label5, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1.0, constant: 0.0).isActive = true
NSLayoutConstraint(item: label5, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1.0, constant: 0.0).isActive = true
}
最佳答案
override func viewDidLoad() {
super.viewDidLoad()
containerView.backgroundColor = .white
let label1 = UILabel()
label1.translatesAutoresizingMaskIntoConstraints = false
label1.text = "label1"
label1.numberOfLines = 0
label1.tag = 1
self.containerView.addSubview(label1)
let label2 = UILabel()
label2.translatesAutoresizingMaskIntoConstraints = false
label2.text = "label2"
label2.numberOfLines = 0
label2.tag = 1
self.containerView.addSubview(label2)
let label3 = UILabel()
label3.translatesAutoresizingMaskIntoConstraints = false
label3.text = "label3"
label3.numberOfLines = 0
label3.tag = 1
self.containerView.addSubview(label3)
let label4 = UILabel()
label4.translatesAutoresizingMaskIntoConstraints = false
label4.text = "label4"
label4.numberOfLines = 0
label4.tag = 1
self.containerView.addSubview(label4)
let label5 = UILabel()
label5.translatesAutoresizingMaskIntoConstraints = false
label5.text = "label5"
label5.numberOfLines = 0
label5.tag = 1
self.containerView.addSubview(label5)
setConstraint(to: [label5, label1, label2, label3, label4])
}
private func setConstraint(to views: [UIView]) {
var iterator = views.makeIterator()
var prev: UIView = self.containerView
while let next = iterator.next() {
NSLayoutConstraint(item: next, attribute: .top, relatedBy: .equal, toItem: prev, attribute: .top, multiplier: 1, constant: 16).isActive = true
NSLayoutConstraint(item: next, attribute: .leading, relatedBy: .equal, toItem: self.containerView, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true
NSLayoutConstraint(item: next, attribute: .trailing, relatedBy: .equal, toItem: self.containerView, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
prev = next
}
}
关于ios - 索引处的 Swift 方法 insertSubview 效果很好。但是如何重新定位所有 subview 以便为插入的 subview 腾出空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44690550/
下类时合上笔记本电脑。当我回到家并打开它时,它已重新启动。现在,当我打开 Titanium Developer 时,它会立即崩溃。 所以我现在也打不开。关于如何调试或找出问题所在的任何想法? 甚至尝试
我们想共享运行时项目二进制文件。所以每个团队成员都可以使用当前的工作版本。 在 SVN 中存储运行时二进制文件是否可以接受/很好? 最佳答案 不,不要将二进制文件存储在其源代码旁边(除非您有充分的理由
我在 PHP 中使用循环来显示 CMS 管理部分中的用户数据。每行(用户)都包含一些我想要连接到命令的图标(例如:编辑、删除等)。表的最后一行有空的输入字段,带有单个图标(命令),以允许添加新用户。这
如果这是一个新手问题,请原谅我,我昨天开始学习 Django,并且我尽量不要养成坏习惯,即我试图从一开始就以“django 方式”做事。 我有一个 View 接收二进制数据作为 http post 字
使用 swift 2.1 我正在寻找一种将非可选类型数组分配给可选类型数组的好方法,其中类型相同。这是我尝试过的一些方法: var foos: [Int?] = [] let bars: [Int]
James Gosling,加拿大计算机科学家,完成了 Java 的原始设计,并实现了 Java 最初版本的编译器和虚拟机,也是公认的 “Java 之父”。 Evrone 是一家企业软件开发公司,
我在两个项目上使用 Twitter Bootstrap,一个是静态 HTML 网站,另一个是 Rails 应用程序。当我在桌面浏览器上测试网站时,调整大小有效。但是当我在手机 [Nokia E72]
我可以使用全日历。但我有一个侧边栏,用户可以折叠它,然后内容框会变大,但是当用户这样做时,日历就不那么好了。 所以我正在考虑窗口调整大小功能,但这仅在浏览器窗口更改时才有效,那么当容器变大或变小时如何
我正在尝试使用 C# 和 LINQ 在数据库中查询每日活跃用户。我有一个运行良好的 SQL 查询。它在 u.UserId).Distinct().Count() } 测试后,当我必须选择 (
我目前正在开发部署目标为 7.1 的 iOS 应用程序。我的大部分测试都是在 iOS8 环境中执行的,没有任何问题。我连接了一个 iOS7 (5s) 测试设备,发现通过手机显示的 View 是 3.5
我有这样的代码: Floating left. Floating right. BlahBlah Container 允许我将页脚推到页面底部,但如果我想让左右栏跨越接触页脚的高度
从 PHP4 和 Cake 1.3 开始,我一直在使用 debug($data);在 CakePHP 中调试诸如模型输出之类的东西。 但是,自从升级到 PHP5.4 后,我注意到 debug($dat
我在Canvas上画我的游戏,一切都是上帝,但我把它改成了JPanel,但现在它不能正常工作,这是代码,你可以复制它们,你就会看到问题出在哪里(我有一个菜单,单击按钮后它应该创建新线程,我想在那里画画
我尝试用 scrapy 抓取一页。我用 FireXpath(一个 firefox 插件)找到了 xpath,它看起来不错。但是对于 Scrapy,我没有得到任何结果。 我的 python 程序如下所示
我想在页面加载时加载 fancybox。它工作正常,但我对它的高度有疑问。假设页面的高度为 3000px,而 fancybox 的高度为 1500px。如果你想看到页面的最低部分,都应该向下滚动。不幸
IE 大小调整问题!?代码非常简单:我有一个 div,我想要一个占 DIV 100% 的文本框。它必须显示 div 的红线(如果我使用 height:100%, width:100% 它会侵 ecli
我有一组看起来像这样的代码: if(self.property == A) { [self.infoManager getThingA:^(NSString *thing, NSError *
我认为 git clone 使用 STDERR。 我现在想将它重定向到 STDOUT 以使用此 hack . 我遇到了一些问题(另外,我使用很棒的 stderred 库自动将 STDERR 着色为红色
我的问题是我的表单正在提交(到节点/express api),如果我console.log req.body(@ api),结果是一个空对象。但是,如果我在客户端上 console.log ,则序列化
我对 addon-sdk 还很陌生,并且遇到了一个对我来说无法解释的问题。到目前为止,我一直在使用 jpm run 来测试一切 - 并且一切都很好。现在,我即将完成我想要完成的任务,所以我想在“普通”
我是一名优秀的程序员,十分优秀!