objective c - Where is the best place to define frame sizes for subviews? -


where programmers specify how big frames supposed be? tried put following code in instance variables section of .h file

    cgrect backbuttonrect = cgrectmake(2 * w/25, 18 * h/25, 6 * w/25, 2 * h/25); 

but won't let me reason. want put parameters in place that's easy remember can debug later.

you cannot initialize instance variable in declaration. that's way objective-c is. it's different java or c# (or c++11) in regard. instance variables initialized zero.

you didn't class contains instance variable.

if you're loading object xib or storyboard, doesn't matter class is; initialized receiving initwithcoder: message. initialize backbuttonrect in initwithcoder:. example:

- (instancetype)initwithcoder:(nscoder *)adecoder {     if ((self = [super initwithcoder:adecoder])) {         cgfloat w = 100, h = 64;         backbuttonrect = cgrectmake(2 * w/25, 18 * h/25, 6 * w/25, 2 * h/25);     }     return self; } 

alternatively, if won't know w , h until of outlets hooked up, initialize backbuttonrect in awakefromnib:

- (void)awakefromnib {     [super awakefromnib];     backbuttonrect = cgrectmake(2 * w/25, 18 * h/25, 6 * w/25, 2 * h/25); } 

if you're creating object in code, matter class is. if it's uiview subclass, designated initializer initwithframe:, that's method override initialization:

- (instancetype)initwithframe:(cgrect)frame {     if ((self = [super initwithframe:frame])) {         cgfloat w = 100, h = 64;         backbuttonrect = cgrectmake(2 * w/25, 18 * h/25, 6 * w/25, 2 * h/25);     }     return self; } 

if it's uiviewcontroller subclass, designated initializer initwithnibname:bundle:.

- (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil {     if (self = [super initwithnibname:nibnameornil bundle:nibbundleornil]) {         cgfloat w = 100, h = 64;         backbuttonrect = cgrectmake(2 * w/25, 18 * h/25, 6 * w/25, 2 * h/25);     }     return self; } 

Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -