UITabbarController 未正确绘制视图

发布于 2024-09-08 04:01:45 字数 883 浏览 1 评论 0原文

我有一个 uitabbarcontroller,其中添加了一个 UIViewController。 (我尽可能减少到最小的情况)。该视图控制器调用一个 UIView,它通过以下方式绘制 UIButton:

- (void)drawRect:(CGRect)rect 
{
    CGRect brect = CGRectMake(0, 330, 60, 27);
    CGPoint centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect) );
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:brect];
    [button setCenter:centern];
    [button setTitle:@"New" forState:UIControlStateNormal];
    [self addSubview:button];
 }

绘制按钮时没有内部文本。只有当我触摸显示屏时,它才会正确绘制。

我的解决方案如下:为选项卡栏控制器提供两个 UIViewController 来控制。然后强制抽出第二个,然后回到第一个:

tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:1];
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:0];

这个解决方案有效,但这是一个愚蠢的黑客。有更好的方法吗?

I have a uitabbarcontroller which has one UIViewController added to it. (I reduced to smallest case possible). This viewcontroller calls up a UIView which draws a UIButton via:

- (void)drawRect:(CGRect)rect 
{
    CGRect brect = CGRectMake(0, 330, 60, 27);
    CGPoint centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect) );
    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button setFrame:brect];
    [button setCenter:centern];
    [button setTitle:@"New" forState:UIControlStateNormal];
    [self addSubview:button];
 }

The button is drawn without the inside text. Only when I touch the display is it then drawn properly.

My solution has been the following: Give the tab-bar controller two UIViewControllers to control. Then force a draw of the second one, and then back to the first one:

tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:1];
tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:0];

This solution works, but it's a stupid hack. Is there a better way to do this?

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

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

发布评论

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

评论(1

调妓 2024-09-15 04:01:45

您发布的代码似乎不适合drawRect:实现。您应该将此代码添加到您的视图 init 实现中,或者添加到您的 viewControllers loadView 或 viewDidLoad 实现中。

这两行没有做任何有用的事情,您可以将它们省略:

CGPoint centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect) );
[button setCenter:centern];

每当您在视图上设置框架时,视图中心都会隐式设置为框架中点(中心)。

The code you posted dosen't seem to fit inside a drawRect: implementation. You should add this code to your views init implementation, or to your viewControllers loadView or viewDidLoad implementation.

These two lines do not do anything useful you can leave them out:

CGPoint centern=CGPointMake(CGRectGetMidX(brect), CGRectGetMidY(brect) );
[button setCenter:centern];

Whenever you set a frame on a view, the views center is implicitly set to the frames midpoint(center).

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