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 でカスタム クラスを使用してアプリを起動すると、ボタンは最初、小さなテキストで小さなものとして一瞬表示され、その後大きくなります。結果は私が望むものですが、移行は見たくありません。誰でも私を正しく言えますか?

ありがとう。JP

4

2 に答える 2

0

この問題は見ていませんが、ボタンの最初のフレームが小さすぎるようです。ボタンがペン先から読み込まれると、ペン先に割り当てられたフレームでボタン自体が描画されます。起動して実行した後、他の要因に対してのみ調整します。

フォント サイズの変更は、通常、初期化中に行われるものではなく、多くの副作用があるため、ボタンが完全に初期化されるまで、クラスは 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 に答える