我正在尝试从我的 View Controller 设置一个 subview 弹出窗口并使用视觉格式约束来定位项目。我希望 subview 看起来像这样,tableView 的高度为 150 pts,imageView 的高度为 100 pts,titleLabel 的高度为 50 pts 并且位于 imageView 的左下角:
为了尝试实现这一点,我使用了以下代码:
override func didMoveToSuperview() {
super.didMoveToSuperview()
// Add views
addSubview(titleLabel)
addSubview(tableView)
addSubview(imageView)
// Setup constraints
heightAnchor.constraint(equalToConstant: 250).isActive = true
var constraints = [NSLayoutConstraint]()
let views: [String: UIView] = ["imageView": imageView, "titleLabel": titleLabel, "tableView": tableView]
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[tableView(150)]|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[titleLabel][tableView]|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[titleLabel(20)]|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[imageView(100)][tableView]|", options: [], metrics: nil, views: views)
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[imageView(100)]|", options: [], metrics: nil, views: views)
NSLayoutConstraint.activate(constraints)
}
但这会导致以下输出(未显示 titleLabel):
最佳答案
问题不在于您的约束。这与您的观点顺序有关。在 imageView 之后添加 titleLabel 使其显示在 imageView 的顶部:
// Add views
addSubview(tableView)
addSubview(imageView)
addSubview(titleLabel)
由于您的 titleLabel 的位置是相对于您的 imageView 的,我建议将其设为 subview 。以下是我建议的限制条件:
override func didMoveToSuperview() {
super.didMoveToSuperview()
// Add views
addSubview(tableView)
addSubview(imageView)
imageView.addSubview(titleLabel)
// Setup constraints
heightAnchor.constraint(equalToConstant: 250).isActive = true
var constraints = [NSLayoutConstraint]()
let views: [String: UIView] = ["imageView": imageView, "titleLabel": titleLabel, "tableView": tableView]
// tableView is 150 wide and stuck to both sides of its superView making the superView 150 wide
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[tableView(150)]|", options: [], metrics: nil, views: views)
// imageView is 100 tall and stuck to top of superView; tableView takes up the rest
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|[imageView(100)][tableView]|", options: [], metrics: nil, views: views)
// imageView is as wide as its superView because it is stuck to both sides
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[imageView]|", options: [], metrics: nil, views: views)
// titleLabel is 20 tall and stuck to the bottom of its superView (notice only one "|")
constraints += NSLayoutConstraint.constraints(withVisualFormat: "V:[titleLabel(20)]|", options: [], metrics: nil, views: views)
// titleLabel is 50 wide and stuck to the left of its superView (again only one "|")
constraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|[titleLabel(50)]", options: [], metrics: nil, views: views)
NSLayoutConstraint.activate(constraints)
}
关于ios - Swift:视觉格式语言约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41428354/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
这个问题在这里已经有了答案:Railsformattingdate(4个答案)关闭4年前。我想格式化Time.Now函数以显示YYYY-MM-DDHH:MM:SS而不是:“2018-03-0909:47:19+0000”该函数需要放在时间中.现在功能。require‘roo’require‘roo-xls’require‘byebug’file_name=ARGV.first||“Template.xlsx”excel_file=Roo::Spreadsheet.open(“./#{file_name}“,extension::xlsx)xml=Nokogiri::XML::Build
几个月前,我读了一篇关于rubygem的博客文章,它可以通过阅读代码本身来确定编程语言。对于我的生活,我不记得博客或gem的名称。谷歌搜索“ruby编程语言猜测”及其变体也无济于事。有人碰巧知道相关gem的名称吗? 最佳答案 是这个吗:http://github.com/chrislo/sourceclassifier/tree/master 关于ruby-寻找通过阅读代码确定编程语言的rubygem?,我们在StackOverflow上找到一个类似的问题: