2017-10-12 68 views
1

在迅速3,我可以做这样的事情,使我UIView角落轮:圆角的UIView斯威夫特4

import UIKit 

@IBDesignable 
class DesignableView: UIView { 
} 

extension UIView { 

    @IBInspectable 
    var cornerRadius: CGFloat { 
     get { 
      return layer.cornerRadius 
     } 
     set { 
      layer.cornerRadius = newValue 
     } 
    } 
} 

,并在故事板,我可以简单地改变这样的: properties

目前我在可设计的情况下得到“构建失败”,但Idk为什么。我正在使用Swift 4和Xcode 9.

为什么它不能在swift 4中工作?

+0

什么问题呢?一切正常。 –

+1

请提供足够的代码让我们重现问题。 – matt

+0

@matt我已更新代码并添加了我在Xcode 8和Swift 3中使用的描述性图像 –

回答

4

我试过你的代码,它的正常工作与iOS 11.1 &雨燕4.0。 (正如你所提到它表明你一个错误,但它不显示我的任何错误)

@IBDesignable 
class RoundUIView: UIView { 

    @IBInspectable var borderColor: UIColor = UIColor.white { 
     didSet { 
      self.layer.borderColor = borderColor.cgColor 
     } 
    } 

    @IBInspectable var borderWidth: CGFloat = 2.0 { 
     didSet { 
      self.layer.borderWidth = borderWidth 
     } 
    } 

    @IBInspectable var cornerRadius: CGFloat = 0.0 { 
     didSet { 
      self.layer.cornerRadius = cornerRadius 
     } 
    } 

} 

下面是结果

enter image description here


更新:
即使你更新后的代码工作正常,也。

@IBDesignable 
class DesignableView: UIView { 
} 

extension UIView { 

    @IBInspectable 
    var cornerRadius: CGFloat { 
     get { 
      return layer.cornerRadius 
     } 
     set { 
      layer.cornerRadius = newValue 
     } 
    } 
} 

下面是结果吧:

enter image description here

+0

简单状态“适用于我”的答案实际上不是可接受的答案堆栈溢出。 – rmaddy

+4

@rmaddy有时需要证明才能显示代码的工作原理。我认为这个答案是完全合理的。 – matt

+0

@matt请看https://meta.stackoverflow.com/questions/277923/are-your-code-works-fine-for-me-answers-acceptable – rmaddy

0

你的代码在新项目中似乎在Swift 4.0中工作正常。但是,if you are using a Storyboard and its set as LaunchScreen too, you won't be able to use custom classes directly there

在这种情况下,只需取消选中即可用作启动屏幕,您应该可以重新构建。

enter image description here

+0

它不在LaunchScreen –

+0

你能分享更多关于构建错误的信息吗?有可能看到详细的消息打开Report Navigator – jvrmed

+0

没有应用程序构建错误,这是一个可设计的构建错误,它不会停止应用程序运行 –