Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIToolBar is Transparent

When I add a UIToolBar, it appears to be transparent. However, I do not want this to happen. Here is my code:

var done = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: Selector("done"))

    if let font = UIFont(name: "Avenir", size: 17.0) {
        done.setTitleTextAttributes([NSFontAttributeName: font], forState: .Normal)
    }
    toolBar.items = [done]
    toolBar.barStyle = UIBarStyle.Default
    self.birthdayTextField.inputAccessoryView = toolBar

Am I doing anything wrong?

like image 748
Rehaan Advani Avatar asked Jul 03 '15 06:07

Rehaan Advani


2 Answers

Having come across this issue myself I found that the toolbar must either be instantiated with a non-zero frame, or have sizeToFit called on it.

e.g.

    let tb = UIToolbar()
    tb.translucent = false
    tb.items = [UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil), UIBarButtonItem.init(title: "A button", style: .Plain, target: self, action: Selector("someAction:"))]
    tb.sizeToFit()
    userField?.inputAccessoryView = tb

or

    let tb = UIToolbar(CGRectMake(0,0,view.frame.width,44))
    tb.translucent = false
    tb.items = [UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil), UIBarButtonItem.init(title: "A button", style: .Plain, target: self, action: Selector("someAction:"))]
    userField?.inputAccessoryView = tb
like image 113
Weaverfish Avatar answered Sep 28 '22 08:09

Weaverfish


try this code for UIToolBar Transparent :

self.toolbar.setBackgroundImage(UIImage(),
                                forToolbarPosition: UIBarPosition.Any,
                                barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
                            forToolbarPosition: UIBarPosition.Any)
like image 30
Jay Bhalani Avatar answered Sep 28 '22 07:09

Jay Bhalani