2

I am re-using a UIView subclass in a few places, but need the layout to be slightly different on occasion - sometimes the subviews are laid out horizontally and sometimes vertically.

I would like to somehow replicate the 'initWithStyle' approach that UITableView and other UIViews employ. It should be possible to 'initWithStyle' but there should be a default also. It should also be possible to change the style using setStyle.

What are the steps to achieve this? I tried defining an enum and a new initializer, but I am out of my depth here.

Wil Shipley
  • 9,343
  • 35
  • 59
Ben Packard
  • 26,102
  • 25
  • 102
  • 183

1 Answers1

0

Sounds like you're on the right track, so I'm not sure how helpful I can be. Create your public initializers and public setStyle methods. Sorry for any typos. I'm doing this without an editor.

.h

-(id)initWithFrame:(CGRect)frame;
-(id)initWithFrame:(CGRect)frame style:(MyStyleEnum)myStyleEnum;
-(void)setStyle:(MyStyleEnum)myStyleEnum;

.m

 //here's your default method which passes your default style to your custom init method
-(id)initWithFrame:(CGRect)frame {
    return [self initWithFrame:frame style:myStyleEnumDefault];
}

//your custom init method that takes a style
-(id)initWithFrame:(CGRect)frame style:(MyStyleEnum)myStyleEnum {
    self = [super initWithFrame:frame];

    if(self) {
        [self setStyle:myStyleEnum];
    }

    return self;
}

//this can be called after initialization
-(void)setStyle:(MyStyleEnum)myStyleEnum {
    //make sure the view is clear before you layout the subviews
    //[self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];//not needed adjusting constraints and not repopulating the view

    //change the view constraints to match the style passed in
    //the acutally changing, should probably be done in separate methods (personal preference) 
    if(myStyleEnum == myStyleEnumDefault) {
       //change constraints to be the default constraints
    } else if(myStyleEnum == myStyleEnumA) {
        //change constraints to be the A constraints
    } else if(myStyleEnum == myStyleEnumB) {
         //change constraints to be the B constraints
    }

}
JeffRegan
  • 1,322
  • 9
  • 25
  • Thanks very much. Since I'm using autolayout, I wonder if I could just remove and add the constraints in setStyle: instead of the subviews themselves? – Ben Packard Jul 28 '13 at 00:21
  • 1
    Sorry, I don't have much experience with xibs or storyboards. It looks like you can and it wouldn't be too much trouble. Here's a link to another question that addresses what you're asking: http://stackoverflow.com/questions/14215641/autolayout-seems-not-working-when-changing-view-dimensions-programmatically Feel free to ping this thread with any more questions. – JeffRegan Jul 28 '13 at 05:18
  • 1
    Also, since you're adjusting the constraints instead of recreating the views, I'm going to fix (it was incorrect) and comment out the remove from super view calls (it's helpful to know, but not really relevant here). – JeffRegan Jul 28 '13 at 05:21
  • I got it up and running and it works well. I was able to switch my constraints easily enough. I don't use xibs or storyboards myself - autolayout seems pretty great in code though. Thanks again. – Ben Packard Jul 28 '13 at 13:02