- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 xib
( childXib
) 文件链接到其自定义 UIView
swift 文件通过其所有者。
这就是我如何初始化我的自定义 UIView
:
// init for IBDesignable
override init(frame: CGRect) {
super.init(frame: frame)
let view = loadViewFromNib()
view.frame = bounds
addSubview(view)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addSubview(loadViewFromNib())
}
func loadViewFromNib() -> UIView {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: "CommentCellView", bundle: bundle)
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
return view
}
xib
(
childXib
) 在另一个
xib
(
parentXib
),我收到以下错误:
error: IB Designables: Failed to render instance of MyRootView: The agent threw an exception.
MyRootView
是链接到
parentXib
的文件
error: IB Designables: Failed to update auto layout status: The agent raised a "NSInternalInconsistencyException" exception: Could not load NIB in bundle: 'NSBundle (loaded)' with name 'MyIBDesignableCustomViewFilename'
MyIBDesignableCustomViewFilename
是链接到
childXib
的文件.
Debug
进行调试时在
Custom class
来自
Identity inspector
,它在该行不起作用:
let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView
xib
文件在
Copy Bundle Resources
在
Build Phases
.
最佳答案
第一步:
我将向您介绍 IBDesignable 和 IBInspectable,并向你们展示如何利用新功能。没有比创建演示更好的方法来详细说明功能。所以我们将一起构建一个名为“Rainbow”的自定义界面。
IBDesignable 和 IBInspectable
使用 IBDesignable 和 IBInspectable,开发人员可以创建在 Interface Builder 中实时呈现的界面(或 View )。一般来说,要应用这个新功能,你需要做的就是通过继承 UIView 或 UIControl 创建一个可视化类,然后在 Swift 中使用 @IBDesignable 关键字作为类名的前缀。如果您使用的是 Objective-C,请改用 IB_DESIGNABLE 宏。这是 Swift 中的示例代码:
@IBDesignable
class Rainbow: UIView {
}
@IBInspectable var firstColor: UIColor = UIColor.blackColor()
{
// Update your UI when value changes
}
@IBInspectable var firstColor: UIColor = UIColor.blackColor()
{
// Update your UI when value changes
}
Then insert the following code in the class:
import UIKit
class Rainbow: UIView {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
}
@IBDesignable
class Rainbow: UIView {
...
}
@IBInspectable var firstColor: UIColor = UIColor(red: (37.0/255.0), green: (252.0/255), blue: (244.0/255.0), alpha: 1.0)
@IBInspectable var secondColor: UIColor = UIColor(red: (171.0/255.0), green: (250.0/255), blue: (81.0/255.0), alpha: 1.0)
@IBInspectable var thirdColor: UIColor = UIColor(red: (238.0/255.0), green: (32.0/255)
func addOval(lineWidth: CGFloat, path: CGPathRef, strokeStart: CGFloat, strokeEnd: CGFloat, strokeColor: UIColor, fillColor: UIColor, shadowRadius: CGFloat, shadowOpacity: Float, shadowOffsset: CGSize) {
let arc = CAShapeLayer()
arc.lineWidth = lineWidth
arc.path = path
arc.strokeStart = strokeStart
arc.strokeEnd = strokeEnd
arc.strokeColor = strokeColor.CGColor
arc.fillColor = fillColor.CGColor
arc.shadowColor = UIColor.blackColor().CGColor
arc.shadowRadius = shadowRadius
arc.shadowOpacity = shadowOpacity
arc.shadowOffset = shadowOffsset
layer.addSublayer(arc)
}
override func drawRect(rect: CGRect) {
// Add ARCs
self.addCirle(80, capRadius: 20, color: self.firstColor)
self.addCirle(150, capRadius: 20, color: self.secondColor)
self.addCirle(215, capRadius: 20, color: self.thirdColor)
}
func addCirle(arcRadius: CGFloat, capRadius: CGFloat, color: UIColor) {
let X = CGRectGetMidX(self.bounds)
let Y = CGRectGetMidY(self.bounds)
// Bottom Oval
let pathBottom = UIBezierPath(ovalInRect: CGRectMake((X - (arcRadius/2)), (Y - (arcRadius/2)), arcRadius, arcRadius)).CGPath
self.addOval(20.0, path: pathBottom, strokeStart: 0, strokeEnd: 0.5, strokeColor: color, fillColor: UIColor.clearColor(), shadowRadius: 0, shadowOpacity: 0, shadowOffsset: CGSizeZero)
// Middle Cap
let pathMiddle = UIBezierPath(ovalInRect: CGRectMake((X - (capRadius/2)) - (arcRadius/2), (Y - (capRadius/2)), capRadius, capRadius)).CGPath
self.addOval(0.0, path: pathMiddle, strokeStart: 0, strokeEnd: 1.0, strokeColor: color, fillColor: color, shadowRadius: 5.0, shadowOpacity: 0.5, shadowOffsset: CGSizeZero)
// Top Oval
let pathTop = UIBezierPath(ovalInRect: CGRectMake((X - (arcRadius/2)), (Y - (arcRadius/2)), arcRadius, arcRadius)).CGPath
self.addOval(20.0, path: pathTop, strokeStart: 0.5, strokeEnd: 1.0, strokeColor: color, fillColor: UIColor.clearColor(), shadowRadius: 0, shadowOpacity: 0, shadowOffsset: CGSizeZero)
}
drawRect
的默认实现方法什么都不做。为了在 View 中绘制圆,我们重写了方法来实现我们自己的绘制代码。
addCircle
方法接受三个参数:arcRadius、capRadius 和 color。
arcRadius
是圆的半径,而
capRadius
是圆顶的半径。
First it draws a half circle at the bottom
Next it draws a full small circle at the edge of the arc.
Finally, it draws the other half of the circle
关于ios - 无法在界面生成器中加载 IBDesignable xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35187169/
我正在尝试实现一个简单的UIButton子类,它是IBDesignable。我希望能够从Interface Builder为控件的每个状态设置颜色。我知道这可以通过IBInspectable关键字实现
我希望能够在代码中布局我的 View Controller ,但可以看到界面构建器中显示的布局。 我知道我可以创建一个 UIView 子类,使其 IBDesignable,并将其分配给 View Co
我正在尝试在我的 IOS 项目中创建一个自定义按钮。但是,当我使用我的类创建按钮时,背景在 Storyboard中不起作用。但是当我运行该程序时,按钮工作正常。 在这里你可以看到我的类(class)
我在使用 XCode 时遇到了一个非常具体的问题。 设置 目标中同名的颜色不同 我的 XCode 项目中有两个目标,目标 A 和 目标 B。 它们都有一个名为 Colors.xcassets 的单独资
我正在使用 Xcode 的本地化导出功能,并且我有一个带有 IBInspectable 字符串的自定义 IBDesignable UIControl。我想知道是否有办法将此字符串包含到导出的 .xli
我使用此视频 (https://www.youtube.com/watch?v=xGdRCUrSu94&t=2502s) 创建了自定义分段控件。我希望能够在运行时将自定义分段控件 (commaSepe
我有一个 IBDesignable 类。 @IBDesignable class Foobar: UIView { // ignore all init code var foo: C
我创建了一个具有自定义 IBDesignable UIView 的框架。当我在项目中只包含框架时,IBInspectable 属性不会显示在属性检查器中,但我可以以编程方式使用 View 属性。 我检
我正在使用 swift 构建一个新的 iOS 应用程序,并尝试制作一个自定义的 IBDesignable UIButton 类以在设计器中使用。我的代码在运行时似乎在构建器或应用程序本身中都不起作用。
我正在尝试使用我导入到项目中的一些 IB 可设计对象,这些可设计对象可通过 Storyboard访问,但在使用时不执行任何操作。我注意到一个 Designables - Build Failed 在右
我是 Xcode 的新手,我认为有办法做到这一点,但我似乎无法弄清楚。 我通过子类化 UIView 并使用 IBDesignable 制作了一个自定义控件。 但是,我似乎无法弄清楚如何将事件从我的控制
我已经创建了 IBDesignable 和 IBInspectable 自定义类来为 View 提供阴影和角半径 但是当我将 Designable 类分配给 view 时,我得到 Designable
当我编写自己的 UIButton 扩展类并使其成为 @IBDesignable 时,我在 Interface Builder 中收到两个错误,即: Main.storyboard: error: IB
我最近对 Assets.xcassets 做了一些更改这对 XCode 造成了绝对的困惑。我最终遇到了多个此类错误:Failed to render and update auto layout
我已经在 OS X 应用程序中实现了一个运行良好的自定义控件。当绘制控件时,它使用来自非系统框架的代码。控件是用swift编写的,框架是用Objective-C编写的。该框架不是与具有自定义控件的应用
我有一个 xib ( childXib ) 文件链接到其自定义 UIView swift 文件通过其所有者。 这就是我如何初始化我的自定义 UIView : // init for IBDesigna
我正在尝试在我的 Objective-C 项目中使用我在 github 上找到的一个 swift 库。我已按照此处的说明进行操作:How to call Objective-C code from S
我不明白为什么在尝试实现@IBDesignable时 socket 为零。该应用程序不断崩溃。 import UIKit import RxSwift import RxCocoa @IBDesign
我有一个 @IBDesignable 自定义 View 。它包含一个带有两个子类 UILabels 的 UIView。 UILabel 的自定义子类正在设置它的字体。 我想要实现的目标是通过将 Vie
我正在尝试创建自定义 UITextField 并设置自定义边框颜色。我也为 borderColor 创建了一个 IBInspectable 属性,如果我使用它的 set 和 get 方法,我可以使用
我是一名优秀的程序员,十分优秀!