Casting a UIBarButtonItem to a UIButton

uibutton not working inside uiview swift
uibarbuttonitem action
uibarbuttonitem action not working
uibarbuttonitem not clickable
uibutton addtarget not working
subview button not clickable swift
button ibaction not working swift
uibutton not tappable

If I want to cast UIToolbar items to a UIBarButtonItem, XCode makes me write it this way:

    for item in toolBar!.items as! [UIBarButtonItem]! {
        // Do something
    }

However, once it does this it then says

"Forced cast from '[UIBarButtonItem]?' to '[UIBarButtonItem]' only unwraps optionals; did you mean to use '!'?"

So, how do I write the above cast to a UIBarButtonItem without XCode showing any errors/warnings?

Look at the declaration of items

var items: [UIBarButtonItem]? { get set }

It's an optional array of UIBarButtonItem, there is no need to cast anything. That's what the warning says.

Just check safely if there are items, UIButton is not involved at all.

if let bar = toolBar, let items = bar.items {
   for item in items {
      // Do something
   }
}

However if the toolbar and its items are designed in Interface Builder and won't be changed at runtime you can force unwrap both.

   for item in toolBar!.items! {
      // Do something
   }

Casting a UIBarButtonItem to a UIButton, If I want to cast UIToolbar items to a UIBarButtonItem, XCode makes me write it this way: for item in toolBar!.items as! [UIBarButtonItem]! { // Do something }  Trying to create a UIBarButtonItem with a custom UIButton, but it is not being displayed: This site uses cookies for analytics, personalized content and ads. By continuing to browse this site, you agree to this use.

You can use if let for safe type casting.

    if let items = toolBar.items {

        for item in items {
            if let btn = item as? UIButton {
                print("'btn' is UIButton.")
            } else {
               print("It is not UIButton.")
            }
        }
    }

UIButton does not respond to event inside a UIBarButtonItem's , UIButton does not respond to event inside a UIBarButtonItem's custom view. Solved! New comments cannot be posted and votes cannot be cast. Sort by. Hook the “Touch Up Inside” event of your UIButton (not the UIBarButtonItem) to call an action method in your controller. Pass the UIButton as the sender, then use [Popover presentPopoverFromRect:[sender frame]] rather than presentPopoverFromBarButtonItem.

An easy way around it without any if / guard letting or force unwrapping is to use forEach instead:

toolbar.items?.forEach { item in
    // do whatever you want
}

Can't adjust position of UIBarButtonItem with a UIButton as its , Can't adjust position of UIBarButtonItem with a UIButton as its custom view? I need to New comments cannot be posted and votes cannot be cast. Sort by. best. I’m using Storyboard editor on XCode 11.3.1 to create a UIButton with an icon. When I add a “system” image to a button, e.g., named square.and.pencil, it shows it in Storyboard and on iOS 13 devices.

you can check if there are items then do a foreach.

// check if toolBar have any items
guard let items = toolBar.items else { return }
// then do a loop with items
for item in items {
    // do something
}

also you can use ?? to provide default value

// provide an empty array if items where nil
for item in toolBar.items ?? [] {
    // do something
}

Get UIButton from UIBarButtonItem iPhone App Development Memo, I guess there is a property like 'button' in UIBarButtonItem class, but it was You can use this as a UIButton object after you cast as UIButton. add a UIButton to UINavigationBar add a UIButton to UINavigationBar In any new iPhone Navigation Based application, how can we add a UIButton to UINavigationBar? UINavigationBar only accepts UIBarButtonItems ..so, see the code below to add

What your warning says is that you are not changing a class (typecasting) but only force-unwrapping. In your case toolBar!.items is a type of [UIBarButtonItem]? and you are converting it to [UIBarButtonItem] which can simply be done by saying toolBar!.items!.

But this has nothing to do with UIButton you mentioned in your title. And you may NOT typecast UIBarButtonItem to UIButton. There is a big difference between the two as UIBarButtonItem is actually instructions to create a button and not a button itself. If you really need to get that button you will need to add it as a custom view using UIBarButtonItem(customView: ) and then extract it as item.customView as? UIButton.

How to add a custom view to a UIBarButtonItem, Most UIBarButtonItems contain either an icon or some text, but they can do so much more – in fact, you can embed any kind of UIView subclass  UIButton height unintentionally increased - Widget - iOS 8. ios,uiview,uibutton,height,ios8-today-widget. Oh urmmm ok I literally just fixed this issue by doing the following: So I basically got rid of the vertical resizing property on the UIView which contains the UIButtons and its fixed.

Cast-enable an iOS app - Codelabs, Google Cast allows users to cast content from a mobile device to a TV. Users can then use their mobile device as a remote control for media playback on the TV. Cast button. Now that the GCKCastContext is initialized, we need to add the Cast button to allow the user to select a Cast device. The Cast SDK provides a Cast button component called GCKUICastButton as a UIButton subclass. It can be added to the application's title bar by wrapping it in a UIBarButtonItem.

Navigation Bar Buttons – iOS Developer, navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:​infoButton]; [[self navigationItem] setTitle:@"Some Title"]; return self; }  bien sûr, personne ne veut cela en ciblant L'AppStore. Une méthode plus peu fiable , et utilise également des caractéristiques non documentées, mais passera le test D'Apple, est de boucler à travers les sous-vues pour rechercher l'article de bouton correspondant.

i want to get uibutton from uibarbuttonitem on ios, CustomView can be obtained by casting with UIButton, but CustomView was null. The reason I want to extract UIButton from UIBarButtonItem is  It was simple enough to mention there, but I felt I should cover type casting in Swift a bit more in-depth in its own post. Type casting is a way to convert an object of one type to another. There is one term that is used a lot when talking about type casting, so it should probably be defined upfront, that term is downcast.

Comments
  • Below is a great collection of interesting answers, and all are very useful, thank helps a lot.