我一直在 XCode 中设计一个自定义 Controller ,它根据它所处的状态绘制自己。在一种状态下, Controller 通过调用 myImage?.draw(in: rect) 来显示图像覆盖的 draw(_ rect: CGRect) 函数。
图像在应用程序中正确显示,但在 Storyboard中没有显示。为了说明这一点,我在同一绘制函数中添加了一个贝塞尔环,它同时显示在应用程序和 Storyboard中:
Controller as displayed in app
Controller as displayed in storyboard
在调查了这个问题后,我认为这可能与图像是可选的这一事实有关。但是,显式展开它(即调用 myImage!.draw(in: rect) 会导致 Storyboard自动布局崩溃:
error: IB Designables: Failed to render and update auto layout status for SermonViewController (1zk-bD-8it): The agent crashed
我已经剥离了类的完整实现:
import UIKit
@IBDesignable class DownloadIndicatorControl: UIView {
let requestDownloadImage = UIImage(named: "requestDownloadImage")
let lineWidth: CGFloat = 3.0
override func draw(_ rect: CGRect) {
// Draw Image
requestDownloadImage?.draw(in: rect)
// Draw ring around image
let radius: CGFloat = fmin(self.bounds.width, self.bounds.height) / 2 - lineWidth / 2
let center: CGPoint = CGPoint(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)
let disk: UIBezierPath = UIBezierPath()
disk.lineWidth = lineWidth
disk.addArc(withCenter: center,
radius: radius,
startAngle: CGFloat(-Double.pi / 2),
endAngle: CGFloat(2 * Double.pi - Double.pi/2),
clockwise: true)
disk.stroke()
}
override init(frame: CGRect){
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
有什么办法可以让图像显示在 Storyboard 中,有什么方法可以显式展开图像而不会使 Storyboard 不断崩溃吗?
最佳答案
UIImage(named:) 方法使用主包,但 Interface Builder 以不同的方式加载资源。
试试这个:
UIImage(命名:in:compatibleWith:)
import UIKit
@IBDesignable class DownloadIndicatorControl: UIView {
var requestDownloadImage: UIImage?
let lineWidth: CGFloat = 3.0
override func draw(_ rect: CGRect) {
// Draw Image
requestDownloadImage?.draw(in: rect)
// Draw ring around image
let radius: CGFloat = fmin(self.bounds.width, self.bounds.height) / 2 - lineWidth / 2
let center: CGPoint = CGPoint(x: self.bounds.size.width / 2, y: self.bounds.size.height / 2)
let disk: UIBezierPath = UIBezierPath()
disk.lineWidth = lineWidth
disk.addArc(withCenter: center,
radius: radius,
startAngle: CGFloat(-Double.pi / 2),
endAngle: CGFloat(2 * Double.pi - Double.pi/2),
clockwise: true)
disk.stroke()
}
override init(frame: CGRect){
super.init(frame: frame)
self.setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.setup()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
self.setup()
}
internal func setup() {
let bundle = Bundle(for: DownloadIndicatorControl.self)
requestDownloadImage = UIImage(named: "download", in: bundle, compatibleWith: nil)
}
}
关于swift - IBDesignable 类绘图 UIImage 未在 Storyboard 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44912956/
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi
C#实现简易绘图工具一.引言实验目的:通过制作窗体应用程序(C#画图软件),熟悉基本的窗体设计过程以及控件设计,事件处理等,熟悉使用C#的winform窗体进行绘图的基本步骤,对于面向对象编程有更加深刻的体会.Tutorial任务设计一个具有基本功能的画图软件**·包括简单的新建文件,保存,重新绘图等功能**·实现一些基本图形的绘制,包括铅笔和基本形状等,学习橡皮工具的创建**·设计一个合理舒适的UI界面**注明:你可能需要先了解一些关于winform窗体应用程序绘图的基本知识,以及关于GDI+类和结构的知识二.实验环境Windows系统下的visualstudio2017C#窗体应用程序三.
如果我在模型中设置验证消息validates:name,:presence=>{:message=>'Thenamecantbeblank.'}我如何让该消息显示在闪光警报中,这是我迄今为止尝试过的方法defcreate@message=Message.new(params[:message])if@message.valid?ContactMailer.send_mail(@message).deliverredirect_to(root_path,:notice=>"Thanksforyourmessage,Iwillbeintouchsoon")elseflash[:error]
我刚刚按照thebootsygempage上的安装说明进行操作在我保存并查看帖子内容之前,一切看起来都不错。这是输出在View中的样子:HeaderSubhead:似乎没有呈现任何html格式,因为它被引号或类似的东西转义了-其他人有这个问题吗?我没有在github页面或SO上看到任何问题来指出我正确的方向。除了遵循gem安装说明之外,我还没有做任何事情,但也许我错过了什么或者只是犯了一个愚蠢的错误。如果你还有什么想知道的,请尽管问。干杯 最佳答案 你需要有这样的东西,转义html: 关