views:

38

answers:

2

Hi,

I have a custom button, subclassed from UIButton that is initialised as below (obviously all the button does is use a custom font).

@implementation HWButton

- (id)initWithCoder:(NSCoder *)decoder {

    if (self = [super initWithCoder: decoder]) {

  [self.titleLabel setFont:[UIFont fontWithName: @"eraserdust" size: self.titleLabel.font.pointSize]];
    }

  return self;
}

So far so good. But when I use the custom class in my nib and launch the app, the button initially displays for a split second as tiny with small text, then grows. So the outcome is what I want, but I don't want to see the transition. Can anyone put me right?

Thanks. JP

A: 

I haven't see this problem but it sounds like the initial frame of the button is way to small. When a button loads from nib, it draws itself with the frame assigned in the nib. It only adjust itself for other factors after it is up and running.

Changing font size isn't something that is normally done during initialization and it has a lot of side effects so the class may well ignore the sizeToFit until the button has completely initialized.

I think the simplest work around for you is to set the frame in IB to the frame it will have with the font you want to use. That way, you shouldn't see the transition at all.

If the button doesn't have to change size once drawn, I would recommend using an image instead of text. Just whip out a Gimped button and be done with it.

TechZen
Thanks - I've noticed also that if I programmatically add (for example) a UIButton and then apply a transform in -ViewDidLoad (to rotate the button slightly) the button appears immediately untransformed and then transforms once it's already displayed. I'm not at my desk now but when I get to it I'll post an example of what I mean.
Jon-Paul
I've transformed a lot of controls and I've never seen that. You want to make sure you put the transforms in the right place like `viewWillAppear` instead of `viewDidAppear`.
TechZen
Sorry that last was just meant as a generic example. Putting the transforms in viewDidLoad should work.
TechZen
A: 

Here's an example of what I'm doing:

In the calling ViewController I use the following code to switch views:

-(void)selectProfile:(User*)selectedUser{
    SelectGameViewController* selectGame=[[SelectGameViewController alloc]initWithNibName:@"SelectGame" bundle:nil];

    UIView* parent=self.view.superview; 

    [UIView beginAnimations:@"Show selection" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [UIView setAnimationDuration:0.50f];
    [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:parent cache:YES];
    [selectGame viewWillAppear:YES];
    [self viewWillDisappear:YES];

    [parent insertSubview:selectGame.view atIndex:0];
    [self.view removeFromSuperview];

    [selectGame viewDidAppear:YES];
    [self viewDidDisappear:YES];
    [UIView commitAnimations];
}

Then in the appearing view, I have the following code in the -viewWillAppear method:

-(void)viewWillAppear:(BOOL)animated{

    UIButton* newButton=[[UIButton alloc]initWithFrame:CGRectMake(50, 150, 500, 150)];
    [newButton setTitle:@"Play" forState:UIControlStateNormal];
    [newButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [newButton.titleLabel setFont:[UIFont fontWithName:@"EraserDust" size:80]];
    [newButton addTarget:self action:@selector(playGame:) forControlEvents:UIControlEventTouchUpInside];
    newButton.transform = CGAffineTransformMakeRotation(-.2);
    [self.view addSubview:newButton];
    [newButton release];


    [super viewWillAppear:animated];
}

The outcome of this is that the view appears with the button unrotated, but then immediately after it's displayed it rotates. I'm very confused as this doesn't seem at odds with TechZen's suggestion?

Jon-Paul