I have a Swift UIView class (named HypnosisView) that draws a circle on the screen. The frame of the view is set to fill the screen. I would like to programmatically set the background color of the view upon initialization (so when an instance of the view is created it automatically has the specified background color). I was able to make this work with a convenience initializer, however I'm wondering if there is a more efficient way to do this (or if in fact I'm doing this correctly). In an ideal scenario, I would like to just add a piece of code that sets the background: self.background = UIColor.clearColor() to the inherited init(frame: CGRect) method, so I don't have to write a whole new initializer just to set the background color. Here is my convenience initializer method (what I'm currently using which works):
convenience init(rect: CGRect){
self.init(frame: rect)
self.backgroundColor = UIColor.clearColor()
}
and I call that method in the delegate like this:
var mainFrame = self.window!.bounds
var mainView = HypnosisView(rect: mainFrame)
Let me know if you have any questions. Thanks!
As discussed in the comments, when wanting to customize the behavior of a UIView, it's often easier to use a convenience initializer as opposed to overriding a designated initializer.
For UIViews specifically, if you override the designated init(frame aRect: CGRect), you are unfortunately also required to override init(coder decoder: NSCoder!) which is part of the NSCoding protocol. So generally if you just want to set a few properties to some default values, do as the original poster asked and create a convenience initializer that in turn calls init(frame aRect: CGRect):
convenience init(rect: CGRect, bgColor: UIColor){
self.init(frame: rect)
self.backgroundColor = bgColor
}
For a discussion on getting rid of NSCoding compliance, see Class does not implement its superclass's required members
Related
Looking at the a lecture slide in the Stanford iOS 9 course here, he is creating a new UIView with two initializers (one if the UIView was created from storyboard, and one if it was created in code). The following code is written at the bottom of that particular slide:
func setup() {....} //This contains the initialization code for the newly created UIView
override init(frame: CGRect) { //Initializer if the UIView was created using code.
super.init(frame: frame)
setup()
}
required init(coder aDecoder: NSCoder) { //Initializer if UIView was created in storyboard
super.init(coder:aDecoder)
setup()
}
The rule is that you must initialize ALL of your own properties FIRST before you can grab an init from a superclass. So why is it that in this case he calls his superclass init super.init BEFORE he initializes himself setup()? Doesn't that contradict the following rule:
Safety check 1 A designated initializer must ensure that all of the properties introduced by its class are initialized before it delegates up to a superclass initializer.
As mentioned above, the memory for an object is only considered fully initialized once the initial state of all of its stored properties is known. In order for this rule to be satisfied, a designated initializer must make sure that all its own properties are initialized before it hands off up the chain.
I haven't seen all the rest of the code in this example, but the rule is only that your properties have to be initialized (i.e. the memory they occupy has to be set to some initial value) before calling super.init(), not that you can't run extra setup code.
You can even get away with sort of not-really-initializing your properties by either declaring your properties lazy var, or using var optionals which automatically initialize to nil. You can then set them after your call to super.init().
For example:
class Foo: UIView {
var someSubview: UIView! // initializes automatically to nil
lazy var initialBackgroundColor: UIColor? = {
return self.someSubview.backgroundColor
}()
init() {
super.init(frame: .zero)
setup() // do some other stuff
}
func setup() {
someSubview = UIView()
someSubview.backgroundColor = UIColor.whiteColor()
addSubview(someSubview)
}
}
I have written a UIStackView subclass, but I am experiencing a strange run-time problem. Here is some sample code where it can be seen:
class SubclassedStackView: UIStackView {
init(text: String, subtext: String) {
let textlabel = UILabel()
let subtextLabel = UILabel()
textlabel.text = text
subtextLabel.text = subtext
super.init(arrangedSubviews: [textlabel, subtextLabel])
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
If you then use it such as this:
let stackView = SubclassedStackView(text: "Test", subtext: "Uh-oh!")
You get a runtime exception with the following message:
fatal error: use of unimplemented initializer 'init(frame:)' for class 'test.SubclassedStackView'
A look at the call stack shows that the base initializer -[UIStackView initWithArrangedSubviews:] is attempting to call init(frame: CGRect) on the subclass, which is intentionally left unimplemented.
Of course I could implement this extra initializer, weird as it would be for it to be called by the superclass, but in my real-life case this would force me to change my properties to use optional types (or implicitly unwrapped optionals) where I shouldn't have to do that.
I could also call init(frame:) instead of init(arrangedSubviews:) and subsequently call addArrangedSubview(view:) to add the arranged subviews. The run-time issue would disappear, but I don't wish to provide a frame.
Why does the superclass's initializer call the subclass's initializer? Can anyone suggest a way to work around this issue without introducing optionals?
Edit: Apple acknowledged this bug which should be fixed in iOS 10. http://www.openradar.me/radar?id=4989179939258368 Still applies to iOS 8-9 unfortunately.
I'm not sure if this will work for your needs, but I've managed to circumvent the problem with an extension on UIStackView:
extension UIStackView {
convenience init(text: String, subtext: String) {
let textlabel = UILabel()
let subtextLabel = UILabel()
textlabel.text = text
subtextLabel.text = subtext
self.init(arrangedSubviews: [textlabel, subtextLabel])
}
}
// ...
let sv = UIStackView(text: "", subtext: "") // <UIStackView: 0x7fcd32022c20; frame = (0 0; 0 0); layer = <CATransformLayer: 0x7fcd32030810>>
A look at the call stack shows that the base initialiser -[UIStackView initWithArrangedSubviews:] is attempting to call init(frame: CGRect) on the subclass, which is intentionally left unimplemented.
Why not just add the missing constructor?
override init(frame: CGRect) {
super.init(frame: frame)
}
The problem's coming from the frame initializer not being available since you've provided your own, and it needs that for its internal implementation. If the frame will be managed by AutoLayout anyways you don't need to be concerned with what it's actually set to initially, so you can just let it perform its internal routines necessary to initialize with your subviews. I don't see from the code above why you would need to add optionals..
init(arrangedSubviews: [] is a convenience initializer. As per documentation, you must call the superclass's designated initializer (which is init(frame:)) instead
I'm using Swift, and I find myself having to subclass UIKit classes such as UIView and UIButton. I don't care about setting the frame since I'm using AutoLayout, so I don't want/need to use init(frame: CGRect).
class customSubclass: UIView {
var logo: UIImage
init(logo: UIImage) {
self.logo = logo
//compiler yells at me since super.init() isn't called before return from initializer
//so I end up doing this
super.init(frame: CGRectZero)
self.translatesAutoresizingMaskIntoConstraints = false
}
I also don't find it very sexy to set it's frame to CGRectZero.
Is there a way to having a custom initializer for a subclass of a UIView or UIButton without explicitly setting it's frame?
Note Every subclass is instantiated in code, so required init(coder: aDecoder) is in my code, but isn't actually doing anything.
Thanks!
During initialization of a subclass, you must call the designated initializer of a superclass. In your case, since you are creating these views programmatically, you must use super.init(frame: CGRect). As you mentioned, it would be useful to implement two designated initializers for your subclass, one of which takes in a frame:CGRect argument.
Please see the accepted answer to this question for a more thorough review.
If you're using autolayout in a storyboard or xib, then just override the init(coder:) method so that it calls the superclass's version, and have your convenience initializers pass CGRectZero or some other value in.
Instead of creating a new class and subclassing, you could try using extensions.
extension UIView {
var logo: UIImage = myImage
func myLogo() {
// code here
}
}
So, as a swift novice, I'm noodling around and learning as I go. Generally a little google foo or a tutorial will help me, but now I'm stumped. So if anyone could explain this to me I would be very happy.
So I'm trying to draw a circle on screen, well, a few of them actually. I found this code online;
(http://www.ioscreator.com/tutorials/drawing-circles-uitouch-ios8-swift)
import UIKit
class CircleView: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext();
//... various stuff to draw a circle ..//
}
}
it's a class that draws a circle when initialised like this
currentCircle = CircleView(frame: CGRectMake(0,0,100,100 ) )
I have a few questions though.
Why does it actually draw a circle? the drawRect function never gets called directly. I guess it's because we're overriding a function in UIView, and I don't understand those concepts yet.
How can a pass variables to the initialisation of that function? Say, I want to draw circles of different thickness and I want to pass an extra variable like so:
currentCircle = CircleView(frame: CGRectMake(0,0,100,100 ), thickness:10 )
How would I modify the init to accept this? adding it like this:
override init(frame: CGRec, thickness: Int) {
super.init(frame: frame)
}
triggers an error (initialiser does override a designated initialiser from its superclass)
And why all the overrides? I've tried making it a class and using the code to draw. However,
CGContextAddArc
triggers a compiler error saying the context isn't valid, so I suppose
UIGraphicsGetCurrentContext()
isn't returning anything useful.
or if anyone knows a useful resource where I can learn a bit more about overriding and initialising classes, that would be welcome.
Why does it actually draw a circle?
You're correct that you never directly call drawRect:. It's called by the system when the view needs to be redrawn. This happens when, for example, the bounds of the view change, or you call setNeedsDisplay()
How would I modify the init…
Your init should look like this:
convenience init(frame: CGRec, thickness: Int) {
self.thickness = thickness
self.init(frame: frame)
}
You can only override methods from your class's superclasses, init(frame: CGRec, thickness: Int) isn't one of them.
You should take time to read Apple's Swift documentation before embarking on tutorials. All you need to know about the language is in there.
For more info on drawing, see Defining a Custom View, here.
An elegant approach is to turn your view into a class with full Interface Builder support. That way you can directly add and configure instances of your view in Interface Builder.
To use your custom view:
Select and insert a view
In the Identity Inspector view, set the Custom Class property to your class name (CircleView)
In the Attributes Inspector view, set the thickness
There are slightly modifications to your code needed, in particular the annotations #IBDesignable and #IBInspectable:
import UIKit
#IBDesignable class CircleView: UIView {
#IBInspectable var thickness: CGFloat = 1
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawRect(rect: CGRect) {
var context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, thickness)
var r = self.bounds
var ellipseRect = CGRectMake(r.origin.x + self.thickness / 2, r.origin.y + self.thickness / 2,
r.size.width - thickness, r.size.height - thickness)
CGContextStrokeEllipseInRect(context, ellipseRect)
}
}
And yes, you're right. You never call 'drawRect:' yourself. You add instances of your view to your screens and iOS will call 'drawRect:' when it needs to paint your view.
It's the overall patterns of an graphical user interfaces system: you mainly react to events such as clicks from the user or repaint events.
This question already has answers here:
Error in Swift class: Property not initialized at super.init call
(12 answers)
Closed 8 years ago.
I am getting Property self.color not initialized as super.init call error with the below code. How can I properly override the init(frame:) function please? I'd like to pass the color along with the init call.
class CircleView: UIView {
// properties
let color: UIColor
init(frame: CGRect, color: UIColor) {
self.color = color
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func drawRect(rect: CGRect) {
...
}
}
Note that any property declared as "let" must be initialized during all possible inits. You are not doing it when the deserialization init is invoked. Thats why it does not compile.
Now that you understand the reason, let's go for the solution. You can make your constant an optional variable, so it does not need to be initialized, which should solve the problem but add another problem: now it is mutable.
If you still want to keep a let (and you probably want, otherwise you would have already defined a var), you need to decode the content from the coder that the second init receives. While doing that you must also override the serialization process and write the color value so your uiview can be properly serialized.
If you are not caring at all about serialization, the first option solves your problem. If you care about what's happening to your code, I suggest going for understanding the serialization API in Swift and implementing the proper init(decoder) and encoder method.