adit
adit

Reputation: 33644

adding UIButton to UIView or UINavigationBar doesn't work

I am trying to add a UIButton to a UINavigationBar with the following:

 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 (i.e: the action in which it's set to is not triggered). 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.

Upvotes: 1

Views: 944

Answers (4)

JomanJi
JomanJi

Reputation: 1417

If you want to add something in the centre in your navigation bar, why don't you just do

navbar.titleView = addSource

Upvotes: 0

Maximilian Litteral
Maximilian Litteral

Reputation: 3089

you have to use a UIBarButton item, heres the code:

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

Upvotes: 0

zaph
zaph

Reputation: 112857

You can not add a UIButton to a UINavigationBar. Use a 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.

and:

A bar button item is a button specialized for placement on a UIToolbar or UINavigationBar object. It inherits basic button behavior from its abstract superclass, UIBarItem. The UIBarButtonItem defines additional initialization methods and properties for use on toolbars and navigation bars.

Upvotes: 1

J S Rodrigues
J S Rodrigues

Reputation: 471

” 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. 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. 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. 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. 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. 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. For more information about implementing a delegate object, see UINavigationBarDelegate Protocol Reference."

from UiNavigationBar class reference.

please also refer UIBarButoonItem -(id)initWithCustomView:(UIView*)view. pleae note UIbutton is subclass of uiview

also refer uinavbaritem's rightBarButtonItem property – setLeftBarButtonItems:animated: – setLeftBarButtonItem:animated: – setRightBarButtonItems:animated: – setRightBarButtonItem:animated: and titleview.

Upvotes: 0

Related Questions