Questions tagged [initwithframe]

The -initWithFrame:(CGRect)frame method is a UIView method on iOS. This method initializes and returns a newly allocated view object with the specified frame rectangle.

This method initializes and returns a newly allocated view object with the specified frame rectangle.

- (id)initWithFrame:(CGRect)aRect

Parameters:
- aRect: The frame rectangle for the view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This method uses the frame rectangle to set the center and bounds properties accordingly.
- Return Value: An initialized view object or nil if the object couldn't be created.

More info:
The new view object must be inserted into the view hierarchy of a window before it can be used. If you create a view object programmatically, this method is the designated initializer for the UIView class. Subclasses can override this method to perform any custom initialization but must call super at the beginning of their implementation.

If you use Interface Builder to design your interface, this method is not called when your view objects are subsequently loaded from the nib file. Objects in a nib file are reconstituted and then initialized using their initWithCoder: method, which modifies the attributes of the view to match the attributes stored in the nib file. For detailed information about how views are loaded from a nib file, see Resource Programming Guide.

Available in iOS 2.0 and later.

16 questions
27
votes
3 answers

Objective C - Custom view and implementing init method?

I have a custom view that I want to be able to initialize both in-code and in nib. What's the correct way to write both initWithFrame and initWithCoder methods? They both share a block of code that is used for some initialization.
aryaxt
  • 76,198
  • 92
  • 293
  • 442
6
votes
2 answers

Conflicting documentation for `[UIView initWithFrame:]`: nullable or nonnull?

Using CLANG_ANALYZER_NONNULL (i.e. -Xclang nullability), I got "Null is returned from a function that is expected to return a non-null value": Using Xcode 7.3 and iOS 9.3 documentation, I checked initWithFrame: and it can return nil: But UIView.h…
Cœur
  • 37,241
  • 25
  • 195
  • 267
3
votes
2 answers

ios: questions regarding init(frame:) and init?(coder:)

Apple's tutorial describes the difference between init(frame:) and init?(coder:) as You typically create a view in one of two ways: by programatically initializing the view, or by allowing the view to be loaded by the storyboard. There’s a…
user8822312
  • 255
  • 4
  • 14
3
votes
1 answer

Cocoa: initWithFrame from a custom view proxy is not being called

According to apple's document Creating a Custom View this method should be called if you're using a custom view proxy in the Interface Builder. If you have not created an Interface Builder palette for your custom view, there are two techniques you…
1
vote
1 answer

InitWithCoder never called for Cocoa NSView based class

I create a XIB. I create this class called MyCustomView and assign it to the XIB's File Owner. - (instancetype)initWithCoder:(NSCoder *)aDecoder{ self = [super initWithCoder:aDecoder]; if (self) [self load]; return self; } -…
Duck
  • 34,902
  • 47
  • 248
  • 470
1
vote
0 answers

Why app crash when initiate view with initWithFrame: method?

Three crash issues reported by Fabric Crashlytics.I checked the Stacktrace,these issues are similar.when initiate UIView(subclass of UIView,UIWebView etc.) my app crash. It is hard to reproduce,because only a few users affected one day. I'll…
myronZhou
  • 21
  • 4
1
vote
1 answer

loadNibNamed vs. initWithFrame dilemma for setting frame's height and width

I created a UIView subclass associated with .xib file. This UIView subclass is to be used in a UIViewController. In the controller, I think there are two options how we instantiate the UIView subclass: MyUIView *myView=[[MyUIView alloc]…
0
votes
1 answer

super initWithFrame returned nil from -traitCollection, which is not allowed

I'm running one of my Objective-C project on XCode 12.0. I have UIControl subclass "SeatingPanelControl" from which another subclass is created named "IpadSeatingPlanControl", wherein I'm getting crash while executing the below code with exception…
iAkshay
  • 1,143
  • 1
  • 13
  • 35
0
votes
1 answer

UICollectionViewCell -- make selectedBackgroundView bigger than cell itself

I am trying to figure out a way to make my selectedBackgroundView of a cell, larger than the cell itself. My following approach aims to make the selectedBackgroundView: the cell's superview width (the entire width of the screen) X the cell's…
0
votes
1 answer

initWithFrame and loadNibNamed not working together?

I'm using Paged UIScrollView with an embedded UIPageControl like explained at https://github.com/romaonthego/REPagedScrollView. The code works fine when I use UIView *page1 = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 280,…
David
  • 213
  • 3
  • 10
0
votes
2 answers

initWithImage works fine initWithFrame is being called more than once

I am sublassing either an UIImageView or UIView for generating simple color tiles with digits. If I am using UIImageView, I use initWithImage method. If I use UIView, the method initWithFrame is being used. Either red square image or…
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
0
votes
1 answer

Sublayer only draws when created in initWithFrame, not initWithCoder

I have a custom view with a sublayer (CAShapeLayer) and subview (UILabel). When I create the layer in initWithCoder and set the background color, it always shows up as black. However, if I move the code into initWithFrame, the color shows up…
dmoss18
  • 867
  • 1
  • 12
  • 25
0
votes
1 answer

initWithFrame is given empty frame

I'm attempting to use a UINavigationController that utilizes a custom subclass of UINavigationBar. However, I'm running into a problem with initWithFrame. When initWithFrame is called, I print out the dimensions and it shows a 0.0 width and 0.0…
0
votes
1 answer

How to save an NSView without it's instance variables being overwritten by initWithFrame:?

A Cocoa newbie here. I was trying to encode an instance variable inside an NSView to save to a file. But whenever I encode it, it is being overwritten to (null) when the initWithFrame: is being called. Is there a way I can skip this behaviour and…
-1
votes
1 answer

Change Frame of Subview in Superview

I have a ViewController that adds an UIView, SpeciesImageView as a subview in viewDidLoad and set constraints in viewWillLayoutSubviews. SpeciesImageView does not have a nib file. When we create speciesImageView in viewDidLoad, it calls…
maddie
  • 1,854
  • 4
  • 30
  • 66
1
2