简体   繁体   English

将UIButton添加到UIView或UINavigationBar不起作用

[英]adding UIButton to UIView or UINavigationBar doesn't work

I am trying to add a UIButton to a UINavigationBar with the following: 我正在尝试使用以下命令将UIButton添加到UINavigationBar中:

 UIButton * addSource = [UIButton buttonWithType:UIButtonTypeCustom];
    [addSource setBackgroundImage:[UIImage imageNamed:@"addsource.png"] forState:UIControlStateNormal];  
    [addSource addTarget: self action:@selector(addSourceButton:) forControlEvents:UIControlEventTouchUpInside]; 
    [addSource setFrame: CGRectMake(115, 5, 32, 32)];  
    [navBar addSubview:addSource];
    [addSource release];

However this doesn't work, any idea? 但是,这行不通吗? When I comment out the setBackgroundImage and change it to setBackgroundColor I can see it, but I can't seem to click on it (ie: the action in which it's set to is not triggered). 当我注释掉setBackgroundImage并将其更改为setBackgroundColor时,我可以看到它,但似乎无法单击它(即:未触发将其设置为的操作)。 Any idea? 任何想法? Changing it to a UIImageView instead of a UIButton also works, I can see the image just fine, so this clarifies that the image is there. 将其更改为UIImageView而不是UIButton也可以,我可以看到图像很好,因此可以澄清图像是否存在。

You can not add a UIButton to a UINavigationBar . 您不能将UIButton添加到UINavigationBar Use a UIBarButtonItem . 使用UIBarButtonItem

From Apple docs: 从苹果文档:

Unlike other types of views, you do not add subviews to a navigation bar directly. 与其他类型的视图不同,您不会将子视图直接添加到导航栏中。 Instead, you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed. 而是使用导航项(UINavigationItem类的实例)来指定要显示的按钮或自定义视图。

and: 和:

A bar button item is a button specialized for placement on a UIToolbar or UINavigationBar object. 条形按钮项目是专门用于放置在UIToolbar或UINavigationBar对象上的按钮。 It inherits basic button behavior from its abstract superclass, UIBarItem. 它从其抽象超类UIBarItem继承了基本的按钮行为。 The UIBarButtonItem defines additional initialization methods and properties for use on toolbars and navigation bars. UIBarButtonItem定义了其他初始化方法和属性,以用于工具栏和导航栏。

如果要在导航栏中的中心添加一些内容,为什么不这样做

navbar.titleView = addSource

” Adding Content to a Navigation Bar ”向导航栏添加内容

When you use a navigation bar as a standalone object, you are responsible for providing its contents. 将导航栏用作独立对象时,您有责任提供其内容。 Unlike other types of views, you do not add subviews to a navigation bar directly. 与其他类型的视图不同,您不会将子视图直接添加到导航栏中。 Instead, you use a navigation item (an instance of the UINavigationItem class) to specify what buttons or custom views you want displayed. 而是使用导航项(UINavigationItem类的实例)来指定要显示的按钮或自定义视图。 A navigation item has properties for specifying views on the left, right, and center of the navigation bar and for specifying a custom prompt string. 导航项的属性用于在导航栏的左侧,右侧和中心指定视图,并用于指定自定义提示字符串。

A navigation bar manages a stack of UINavigationItem objects. 导航栏管理UINavigationItem对象的堆栈。 Although the stack is there mostly to support navigation controllers, you can use it as well to implement your own custom navigation interface. 尽管该堆栈主要用于支持导航控制器,但是您也可以使用它来实现自己的自定义导航界面。 The topmost item in the stack represents the navigation item whose contents are currently displayed by the navigation bar. 堆栈中最顶层的项目表示导航项目,其内容当前由导航栏显示。 You push new navigation items onto the stack using the pushNavigationItem:animated: method and pop items off the stack using the popNavigationItemAnimated: method. 您可以使用pushNavigationItem:animated:方法将新的导航项目推入堆栈,并使用popNavigationItemAnimated:方法将新项目弹出堆栈。 Both of these changes can be animated for the benefit of the user. 这两个更改都可以动画化,以使用户受益。

In addition to pushing and popping items, you can also set the contents you could also use of the stack directly using either the items property or the setItems:animated: method. 除了推送和弹出项目外,还可以使用items属性或setItems:animated:方法直接设置还可以使用堆栈的内容。 You might use these methods at launch time to restore your interface to its previous state or to push or pop more than one navigation item at a time. 您可以在启动时使用这些方法将界面恢复到其先前的状态,或者一次推入或弹出一个以上的导航项。

If you are using a navigation bar as a standalone object, you should assign a custom delegate object to the delegate property and use that object to intercept messages coming from the navigation bar. 如果将导航栏用作独立对象,则应将自定义委托对象分配给委托属性,并使用该对象来拦截来自导航栏的消息。 Delegate objects must conform to the UINavigationBarDelegate protocol. 委托对象必须符合UINavigationBarDelegate协议。 The delegate notifications let you track when navigation items are pushed or popped from the stack. 委托通知使您可以跟踪何时从堆栈中推入或弹出导航项。 You would use these notifications to update the rest of your application's user interface. 您将使用这些通知来更新应用程序的其余用户界面。

For more information about creating navigation items, see UINavigationItem Class Reference. 有关创建导航项的更多信息,请参见UINavigationItem类参考。 For more information about implementing a delegate object, see UINavigationBarDelegate Protocol Reference." 有关实现委托对象的更多信息,请参见UINavigationBarDelegate协议参考。

from UiNavigationBar class reference. 从UiNavigationBar类参考。

please also refer UIBarButoonItem -(id)initWithCustomView:(UIView*)view. 也请参考UIBarButoonItem-(id)initWithCustomView:(UIView *)视图。 pleae note UIbutton is subclass of uiview pleae note UIbutton是uiview的子类

also refer uinavbaritem's rightBarButtonItem property – setLeftBarButtonItems:animated: – setLeftBarButtonItem:animated: – setRightBarButtonItems:animated: – setRightBarButtonItem:animated: and titleview. 还请参考uinavbaritem的rightBarButtonItem属性– setLeftBarButtonItems:动画:– setLeftBarButtonItem:动画:– setRightBarButtonItems:动画:– setRightBarButtonItem:动画:和titleview。

you have to use a UIBarButton item, heres the code: 您必须使用UIBarButton项目,以下是代码:

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(dismissView)];
    self.navigationItem.leftBarButtonItem = cancelButton;
    [cancelButton release];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM