UIBarButtonItem中的UIButton没有显示出来

时间:2022-12-09 20:06:02

I have the following in my view did load on my table view controller:

我在视图中有以下内容加载到我的表视图控制器上:

UIButton *change_view = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[change_view setTitle:@"List" forState:UIControlStateNormal];
[change_view addTarget:self action:@selector(toggleView:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView: change_view];    
self.navigationItem.rightBarButtonItem = button;    

When I run the program, it doesn't show any title or the rounded rectangle button. However, if I change the type to UIButtonTypeInfoLight, it will show up... so what is the problem?

当我运行程序时,它不显示任何标题或圆角矩形按钮。但是,如果我将类型更改为UIButtonTypeInfoLight,它将显示...所以问题是什么?

2 个解决方案

#1


6  

Try to set an appropriate frame to your change_view.

尝试为change_view设置合适的框架。

When you use UIButtonTypeInfoLight type button uses some built-in size (likely depended on icon used for it), with UIButtonTypeRoundedRect type default frame is applied which must be CGRectZero rect.

当您使用UIButtonTypeInfoLight时,类型按钮使用一些内置大小(可能依赖于用于它的图标),使用UIButtonTypeRoundedRect类型应用默认帧,它必须是CGRectZero rect。

#2


16  

I'd prefer the way below to set the button's bounds.

我更喜欢以下方式来设置按钮的界限。

[button setTitle:@"Title" forState:UIControlStateNormal];
[button sizeToFit];

#1


6  

Try to set an appropriate frame to your change_view.

尝试为change_view设置合适的框架。

When you use UIButtonTypeInfoLight type button uses some built-in size (likely depended on icon used for it), with UIButtonTypeRoundedRect type default frame is applied which must be CGRectZero rect.

当您使用UIButtonTypeInfoLight时,类型按钮使用一些内置大小(可能依赖于用于它的图标),使用UIButtonTypeRoundedRect类型应用默认帧,它必须是CGRectZero rect。

#2


16  

I'd prefer the way below to set the button's bounds.

我更喜欢以下方式来设置按钮的界限。

[button setTitle:@"Title" forState:UIControlStateNormal];
[button sizeToFit];