This is a 3rd library custom control:
However, my custom control puts all properties together. I wonder how to add separators among #IBInspectable properties to show in interface builder?
Related
For an iOS9+/Swift 3 app with many UIViewControllers, what is the best practice way of inheriting the same background colors and other common attributes (fonts, spacing, etc...). Is it best to have a common base class? Or some sort of protocol?
Because most visual attributes are associated with specific UIViews and UIView subclasses (like UILabel, UIButton, etc.), I would suggest that having this handled at the UIViewController level is forcing the view controller to be responsible for too many details specific to other classes.
Apple's built-in mechanism for setting global visual attributes on various views is the UIAppearance API. There's a good overview of that on NSHipster
The downside of using UIAppearance is that is doesn't easily accommodate variations in styling, like some buttons being red and some buttons being blue. It's basically a fixed default appearance for all instances of a certain view across the app.
I personally prefer using a dedicated styling framework that allows for applying combinations predefined styles to views. I have written about this and also created a framework to facilitate creating styles for UIKit components and applying and previewing them inside storyboards.
You could have a constants file with UI constants. Just refer to those variables in all view controllers and if you need to make changes everything else will change too.
You could also have an extension for certain things. I usually have an extension on UIColor that returns a color and I can change it in the extension and it will change everywhere else.
e.g.
extension UIColor{
class func selectedBlue() -> UIColor {
return UIColor(red:0.25, green:0.58, blue:0.97, alpha:1)
}
}
You can have a common base class for a desired component e.g for setting background color of your views, Create a subclass let its name be BackgroundView.
class UHBView: UIView {
override func awakeFromNib() {
super.awakeFromNib()
self.backgroundColor = UIColor.blueColor(); //Any color you want
}
}
Now set this subclass as a custom class in identity inspector in Interface Builder for any view.
I want to create a sub-class of UIButton. I want to give some extra functionality there. Here in this class i am using the enum also like as UIButtonType in the UIButton class. I want to show this option in the storyboad UI also, from where developer can select the type of custom button class. Please suggest me, how I can achieve this.
You need to specify your UIButton inherited class as IBDesignable and to add a NSInteger IBInspectable property that will be analyzed by InterfaceBuilder in order to be displayed.
However, Interface Builder can not render a enum type IBInspectable property; that's why the only way to achieve this is an NSInteger property.
Finally you will select a integer value in interface builder for your custom button (0, 1 ... YourEnumMaxValue) that corresponds to the enum value. At last, you just need to implement some code to render your button following the choosen value in IB.
More on LiveRendering : Creating a Custom View That Renders in Interface Builder
I'm trying to make a framework of a custom UIViewController using #IBInspectable to edit its properties in Interface Builder.
public class BWWalkthroughPageViewController: UIViewController {
#IBInspectable public var speed: CGPoint = CGPointZero
#IBInspectable public var speedVariance: CGPoint = CGPointZero
#IBInspectable public var animationType: String = ""
#IBInspectable public var animateAlpha: Bool = false
}
If the framework source code is in a sample app project, the properties come up to Attribute Inspector as shown in the image below,
but if I add the built framework to my app project, the properties are not displayed in the inspector.
Is it possible to show the inspectable properties in a built framework to Interface Builder? If possible, how to?
Here is the framework source code, which is forked from a non-framework project.
https://github.com/yoichitgy/BWWalkthrough/tree/support-carthage
Creating framework
Create default framework and add there your swift files.
Then set your swift files as public.
After that copy your framework and add it to your project folder
Use the framework
Add the framework in your project
Create new viewController in your storyboard and set viewController's class which is located in your framework
Thats all. You will see your your properties in the storyboard.
The reason the attributes won't show up in Interface Builder in your app but do in your framework is because your framework isn't a target of your app. The easiest way to ensure that it is included is to use Cocoapods, it'll handle everything nicely for you and the setup is relatively easy.
A side note: IBDesignable and IBInspectable really don't have anything to do with each other. With IBInspectable, you can expose a limited amount of property types to Interface Builder or a Storyboard (things like NSNumber, Bool, UIColor, etc). IBDesignable however is used to live render your views in a nib or storyboard. They're very handy to use together i.e. changing properties for your inspectable attributes and seeing your view rendered.
I'm using new Xcode 6 feature called "LiveRendering".
As i need to use some inspectable properties a lot on different custom views, i want to declare them in protocols.
For example :
LiveRenderingTextAttributesProtocol (that declares inspectable properties for textColor, textSize
LiveRenderingBorderAttributesProtocol (that declares inspectable properties for borderStyle, borderColor, borderWidth)
etc ...
After that, each custom view implements the protocols it needs.
But i can't see my inspectable properties on InterfaceBuilder "Attributes inspector" column :/ The LiveRendering is working well when I define the values for these properties as "Used Defined Runtime Attributes", but i want to see them in my Attributes inspector column.
An idea to solve this problem please ?
A protocol defines a set of optional or required methods and attributes to be compliant with, but it's the class the responsible for implementing them. To be able to have reusable base IBInspectable properties you could have a superclass like MyInspectableView that has those properties declared and implemented, so you can subclass it and have those IBInspectables shared between all of them.
Making UI programmatically is very powerful.
But is there a way I would have missed to see for instance CG Graphics drawing in IB without to have to compile the code ?
I guess no, but I'd prefer to ask to refer that after :)
Nop, how would IB know what to draw without compiling the code?
The new answer to this question is: Yes.
With Xcode 6, you can make Interface Builder render your custom views. In addition, you can also make Interface Builder-inspectable properties on custom views that you can set from Interface Builder just like properties of built-in views.
All you need to do is add the line IB_DESIGNABLE just above the class declaration in the header. This will make Interface Builder render your custom view by invoking its drawRect: method.
If you also want the inspectable properties, just add IBInspectable to the property declarations such as: #property IBInspectable float myValue, and you will be able to set that property directly from Interface Builder.
With the combination of those two, I am currently designing a custom view and by changing the properties (color, stroke width etc) I can see the effects in realtime, which is probably just you were asking for.
It came a bit late, but finally, it's here.