我有几个部分透明的 PNG 叠加层,以在 View 中相互叠加显示。覆盖的数量因运行时条件而异。我想在 Swift 中创建 UIImageView 实例并将它们添加到 View 中。
在 Storyboard 中添加 UIImageView 并通过 Swift 设置其图像时,我可以轻松获得所需的行为。在这种情况下,我将 UIImageView 的前导、尾随、顶部和底部固定到父包装器 UIView 的前导、尾随、顶部和底部。在 UIImageView 的属性检查器中,我将“Content Mode”设置为“Aspect Fit”,当 Swift 设置图像时,大图像会很好地缩小。用户可以在 iPhone 的小屏幕上看到整个图像。
当我动态创建一个 UIImageView 并将其添加到同一父包装器 UIView 时,我遇到了约束问题。需要满足的约束条件太多。
我不明白为什么通过 Swift 创建的 UIImageView 与通过 Storyboard创建的 UIImageView 不同。
如何让通过 Swift 创建的 UIImageView 的行为与通过 Storyboard创建的 UIImageView 一样?
在通过 Storyboard创建的 UIImageView 和通过 Swift 创建的 UIImageView 上设置图像的 Swift 代码。
override func viewDidLoad() -> Void {
super.viewDidLoad();
let imageFilePath: String = Bundle.main.resourceURL!.appendingPathComponent("img/a.png", isDirectory: false).path;
// Use the outlet to set the image in the UIImageView created in the storyboard.
// This works.
self.storyboardImageView.image = UIImage(contentsOfFile: imageFilePath);
// Create a UIImageView programatically and add it to the same wrapper UIView
// that also contains the UIImageView above.
// This doesn't work.
let swiftImageView: UIImageView = UIImageView(image: UIImage(contentsOfFile: imageFilePath));
// The presence of the following line makes no difference
//
swiftImageView.contentMode = .scaleAspectFill;
self.storyboardWrapperView.addSubview(swiftImageView);
swiftImageView.topAnchor.constraint(equalTo: self.storyboardWrapperView.topAnchor).isActive = true;
swiftImageView.bottomAnchor.constraint(equalTo: self.storyboardWrapperView.bottomAnchor).isActive = true;
swiftImageView.leadingAnchor.constraint(equalTo: self.storyboardWrapperView.leadingAnchor).isActive = true;
swiftImageView.trailingAnchor.constraint(equalTo: self.storyboardWrapperView.trailingAnchor).isActive = true;
当布局管理器试图满足冲突约束时,我在 Xcode 中看到的输出。
2017-05-17 22:45:18.351141-0700 My App[29787:7263069] [LayoutConstraints] 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)
(
"<NSAutoresizingMaskLayoutConstraint:0x170096670 h=--& v=--& UIImageView:0x101432d20.midX == 540 (active)>",
"<NSLayoutConstraint:0x174092520 H:|-(0)-[UIView:0x101439120] (active, names: '|':UIView:0x10142f4a0 )>",
"<NSLayoutConstraint:0x174092570 H:[UIView:0x101439120]-(0)-| (active, names: '|':UIView:0x10142f4a0 )>",
"<NSLayoutConstraint:0x1740936a0 H:|-(0)-[UIImageView:0x101432d20] (active, names: '|':UIView:0x101439120 )>",
"<NSLayoutConstraint:0x174093790 UIImageView:0x101432d20.trailing == UIView:0x101439120.trailing (active)>",
"<NSLayoutConstraint:0x17009b260 'UIView-Encapsulated-Layout-Width' UIView:0x10142f4a0.width == 375 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x174093790 UIImageView:0x101432d20.trailing == UIView:0x101439120.trailing (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2017-05-17 22:45:18.360996-0700 My App[29787:7263069] [LayoutConstraints] 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)
(
"<_UILayoutSupportConstraint:0x174091cb0 _UILayoutGuide:0x10143e8d0.height == 0 (active)>",
"<_UILayoutSupportConstraint:0x174092930 V:|-(0)-[_UILayoutGuide:0x10143e8d0] (active, names: '|':UIView:0x10142f4a0 )>",
"<_UILayoutSupportConstraint:0x174091da0 _UILayoutGuide:0x10143ddc0.height == 0 (active)>",
"<_UILayoutSupportConstraint:0x174091490 _UILayoutGuide:0x10143ddc0.bottom == UIView:0x10142f4a0.bottom (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x17009cc50 h=--& v=--& UIImageView:0x101432d20.midY == 877.5 (active)>",
"<NSLayoutConstraint:0x174092660 V:[_UILayoutGuide:0x10143e8d0]-(0)-[UIView:0x101439120] (active)>",
"<NSLayoutConstraint:0x1740927a0 V:[UIView:0x101439120]-(0)-[_UILayoutGuide:0x10143ddc0] (active)>",
"<NSLayoutConstraint:0x174093380 V:|-(0)-[UIImageView:0x101432d20] (active, names: '|':UIView:0x101439120 )>",
"<NSLayoutConstraint:0x174093650 UIImageView:0x101432d20.bottom == UIView:0x101439120.bottom (active)>",
"<NSLayoutConstraint:0x170098830 'UIView-Encapsulated-Layout-Height' UIView:0x10142f4a0.height == 667 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x174093650 UIImageView:0x101432d20.bottom == UIView:0x101439120.bottom (active)>
Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
当我使用 Xcode 调试 Debug View Hierarchy 时
以下链接图片显示:
最佳答案
在您的控制台日志中,这两行告诉您发生了什么:
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
.
NSAutoresizingMaskLayoutConstraint:0x17009cc50 h=--& v=--& UIImageView:0x101432d20.midY == 877.5 (active)
将这一行添加到您的代码中将解决问题:
swiftImageView.translatesAutoresizingMaskIntoConstraints = false
关于ios - UIImageView .scaleAspectFit 和自动布局无法从 Swift 以编程方式工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44039852/
很好奇,就使用rubyonrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
这里有一个很好的答案解释了如何在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返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
是否可以为特定(或所有)项目使用多个布局?例如,我有几个项目,我想对其应用两种不同的布局。一个是绿色的,一个是蓝色的(但是)。我想将它们编译到我的输出目录中的两个不同文件夹中(例如v1和v2)。我一直在玩弄规则和编译block,但我不知道这是怎么回事。因为,每个项目在编译过程中只编译一次,我不能告诉nanoc第一次用layout1编译,第二次用layout2编译。我试过这样的东西,但它导致输出文件损坏。compile'*'doifitem.binary?#don’tfilterbinaryitemselsefilter:erblayout'layout1'layout'layout2'