0

我有一个自定义按钮,从 UIButton 子类化,初始化如下(显然所有按钮都使用自定义字体)。

@implementation HWButton

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

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

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

  return self;
}

到现在为止还挺好。但是当我在我的 nib 中使用自定义类并启动应用程序时,按钮最初会显示一小段时间,并带有小文本,然后会变大。所以结果是我想要的,但我不想看到过渡。任何人都可以让我正确吗?

谢谢。J.P

4

2 回答 2

0

我没有看到这个问题,但听起来按钮的初始框架太小了。当一个按钮从 nib 加载时,它会使用 nib 中分配的框架来绘制自己。它仅在启动并运行后才针对其他因素进行自我调整。

更改字体大小通常不会在初始化期间完成,并且它有很多副作用,因此该类可能会忽略 sizeToFit,直到按钮完全初始化。

我认为对您来说最简单的解决方法是将 IB 中的框架设置为您要使用的字体将具有的框架。这样,您根本不应该看到过渡。

如果按钮在绘制后不必更改大小,我建议使用图像而不是文本。只需抽出一个 Gimped 按钮即可完成。

于 2010-06-07T21:07:07.753 回答
0

这是我正在做的一个例子:

在调用 ViewController 时,我使用以下代码来切换视图:

-(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];
}

然后在出现的视图中,我在 -viewWillAppear 方法中有以下代码:

-(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];
}

这样做的结果是视图显示时按钮未旋转,但在显示后立即旋转。我很困惑,因为这似乎与 TechZen 的建议不一致?

于 2010-06-08T16:47:57.060 回答