ViewDidLoad 期间视图跳转

发布于 2024-09-05 01:16:45 字数 443 浏览 8 评论 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;
}

到目前为止,一切都很好。但是,当我在笔尖中使用自定义类并启动应用程序时,该按钮最初会短暂显示为很小的文本,然后会变大。所以结果是我想要的,但我不想看到转变。谁能帮我纠正一下吗?

谢谢。 太平绅士

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过气美图社 2024-09-12 01:16:46

我还没有看到这个问题,但听起来按钮的初始框架太小了。当按钮从笔尖加载时,它会使用笔尖中分配的框架来绘制自身。它只会在启动并运行后根据其他因素进行自我调整。

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

我认为对您来说最简单的解决方法是将 IB 中的框架设置为您想要使用的字体的框架。这样,您根本就不会看到转换。

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

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.

太傻旳人生 2024-09-12 01:16:46

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

在调用 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 的建议并不矛盾?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文