如何以编程方式将 UIButton 添加到 UIToolBar?
我已经使用 Interface Builder 添加了工具栏,但我需要在运行时/有条件地添加按钮。我没有收到任何错误,但我的动态按钮没有出现在工具栏上。我已经验证 arrayOfModulesScreens 已加载我需要的数据。至少那是有效的(:))。我是否需要将按钮添加到 UIView 中,然后将该视图添加到工具栏?只是想出来而已。也许有更好的方法开始?预先感谢您提供任何有助于解决问题的线索。
CustomFormController.h
@interface CustomFormController : UIViewController {
UIToolbar *screensBar;
}
CustomFormController.m
EPData *epData = [[EPData alloc] init];
NSArray *screens = [epData loadPlistIntoArray:@"Screens"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"process_module_id == %@", process_modulesID];
NSArray *arrayOfModulesScreens = [screens filteredArrayUsingPredicate:predicate];
for(int i=0; i < [arrayOfModulesScreens count]; i++) {
NSDictionary *dictRow = [arrayOfModulesScreens objectAtIndex:i];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[button setTitle:[dictRow objectForKey:@"screen_title"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[screensBar addSubview:button];
}
I have added the toolbar using Interface Builder, but I need to add the buttons at runtime / conditionally. I'm not getting any errors, but my dynamic buttons are not appearing on the toolbar. I have verified that arrayOfModulesScreens is loaded with the data I need. At least that works (:)). Do I need to add the buttons into a UIView, then add that view to the toolbar? Just thinking outloud. Perhaps there is a better approach to begin with? Thanks in advance for any clues leading to the resolve.
CustomFormController.h
@interface CustomFormController : UIViewController {
UIToolbar *screensBar;
}
CustomFormController.m
EPData *epData = [[EPData alloc] init];
NSArray *screens = [epData loadPlistIntoArray:@"Screens"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"process_module_id == %@", process_modulesID];
NSArray *arrayOfModulesScreens = [screens filteredArrayUsingPredicate:predicate];
for(int i=0; i < [arrayOfModulesScreens count]; i++) {
NSDictionary *dictRow = [arrayOfModulesScreens objectAtIndex:i];
UIButton *button = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
[button setTitle:[dictRow objectForKey:@"screen_title"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[screensBar addSubview:button];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您实际上想向工具栏添加 UIBarButtonItem(不是 UIButton),只需创建一个或多个 UIBarButtonItem,将它们放入 NSArray(或 NSMutableArray)中,然后将该数组分配给
items
工具栏的属性。有关详细信息,请参阅UIBarButtonItem 文档 。使用上面的代码,可能看起来像这样:(当然,然后您需要更改
buttonClick:
以期望 UIBarButtonItem 而不是 UIButton)。如果您确实想在其中放置一个 UIButton,则首先需要将 UIButton 包装在 UIBarButtonItem 中,如下所示:
然后将项目添加到工具栏,如上所示。
至于为什么您的按钮没有显示在您发布的代码中,问题是 UIButton 的
buttonWithType:
创建了一个零宽度和零高度的按钮。您需要调整按钮的大小(手动或在设置标题后使用sizeToFit
)以使其显示。修复此问题后,您会看到按钮全部位于父视图左上角的彼此之上;您需要根据需要手动放置它们。If you are actually wanting to add a UIBarButtonItem (not a UIButton) to the toolbar, you just create one or more UIBarButtonItems, put them in an NSArray (or NSMutableArray), and assign that array to the
items
property of the toolbar. See the UIBarButtonItem documentation for details. Using your code above, that might look something like this:(you would, of course, then need to change your
buttonClick:
to expect a UIBarButtonItem instead of a UIButton).If you're really wanting to put a UIButton in there, you first need to wrap the UIButton in a UIBarButtonItem, something like this:
Then add the item to the toolbar as above.
As for why your buttons aren't showing up in your posted code, the problem is that UIButton's
buttonWithType:
creates a button with zero width and zero height. You would need to resize the button (manually or by usingsizeToFit
after setting the title) to make it show. After fixing that, you would then see that the buttons are all on top of each other in the upper-left corner of the parent view; you would need to position them manually as appropriate.我认为您不能像这样直接在代码中更改
navigationController
。更安全的方法是这样的:
I don't think you can change the
navigationController
directly in your code like that.A safer way would be this:
ToolBar 方法
将
UIButton
添加到UIToolBar
的一个问题是按钮的宽度和按钮的位置很难控制更简单的方法
删除工具栏并放置一个
UIView
,其背景图像与工具栏相同。现在您可以将UIButton
添加为该视图的子视图。这样您就可以更好地控制按钮的位置和宽度。您也可以添加其他视图。ToolBar Approach
One problem with adding
UIButton
toUIToolBar
is that width of the button and positioning of the buttons is difficult to controlSimpler Approach
Remove the toolbar and place a
UIView
with the background image which is identical to your toolbar. Now you can add theUIButton
as a subview to that view. This way you have more control over the positioning and the width of the buttons. Also you can add other views as well.