Silver Challenge


#1

This was a pretty cool challenge to tackle. At first when I implemented it my Slider and Buttons were not autosizing correctly. In fact, they were disappearing off the screen. I went back to the section on pg 182 and after re-reading it a few times I realized that for the “struts”, you need to select the opposite constant. I created an outlet for each of the subview’s and wired them up to the File Owner.

Here is my solution:

HeavyViewController.h

@interface HeavyViewController : UIViewController
{
    IBOutlet UIView *mainView;
    __weak IBOutlet UIImageView *image;
    __weak IBOutlet UISlider *slider;
    __weak IBOutlet UIButton *leftButton;
    __weak IBOutlet UIButton *rightButton;
}

HeavyViewController.m

//Set the autoresizing mask programmatically
- (void)viewDidLoad
{
    //Image view
    [image setAutoresizingMask:UIViewAutoresizingFlexibleHeight
     | UIViewAutoresizingFlexibleWidth];
    
    //Slider
    [slider setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin
     | UIViewAutoresizingFlexibleBottomMargin
     | UIViewAutoresizingFlexibleWidth];
    
    //Left Button
    [leftButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin
     | UIViewAutoresizingFlexibleTopMargin];
    
    //Right Button
    [rightButton setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin
     | UIViewAutoresizingFlexibleTopMargin];
}

#2

Thanks for this.

I did the same but I created an init method for HeavyViewController and tried to set the resizing mask there. It did not work.

But it does work when I set the mask in the viewDidLoad method. I don’t get exactly why but well …


#3

Read the View Controller Lifecycle stuff in the previous chapter - you can’t send messages to views in init because views aren’t created when a view controller is initialized. Also, a view controller will have a number of different views over its lifetime, but can only be initialized once.


#4

[quote=“DidItSave”]HeavyViewController.m

//Slider [slider setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleWidth]; } [/quote]
You don’t need UIViewAutoresizingFlexibleTopMargin here. You’ll see a bug if run your code on iPad simulator.


#5

I set different tag for each subview and used -(UIView *)viewWithTag: to return the view. For example, the slider was set with tag 0, and autoresized it within viewDidLoad. But I am not sure if there is a better way. Anyone wants to comment?

codeviewDidLoad
{
//slider
UIView *viewSlider = [[self view] viewWithTag:0];
[viewSlider setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleWidth];
[/code]


#6

Can somebody upload the solution to this challenge to github


#7

[quote=“JamesLiu”]I set different tag for each subview and used -(UIView *)viewWithTag: to return the view. For example, the slider was set with tag 0, and autoresized it within viewDidLoad. But I am not sure if there is a better way. Anyone wants to comment?

codeviewDidLoad
{
//slider
UIView *viewSlider = [[self view] viewWithTag:0];
[viewSlider setAutoresizingMask:UIViewAutoresizingFlexibleBottomMargin |
UIViewAutoresizingFlexibleWidth];
[/code][/quote]

Sure…

Can you explain why you did it this way as it seems like there is more code to type?