UITabbarController 未正确绘制视图
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您发布的代码似乎不适合drawRect:实现。您应该将此代码添加到您的视图 init 实现中,或者添加到您的 viewControllers loadView 或 viewDidLoad 实现中。
这两行没有做任何有用的事情,您可以将它们省略:
每当您在视图上设置框架时,视图中心都会隐式设置为框架中点(中心)。
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:
Whenever you set a frame on a view, the views center is implicitly set to the frames midpoint(center).