UIToolBar is Transparent
Asked Answered
M

5

6

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?

Maidamaidan answered 3/7, 2015 at 6:47 Comment(0)
T
4

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
Trautman answered 23/2, 2016 at 12:40 Comment(0)
C
2

try this code for UIToolBar Transparent :

self.toolbar.setBackgroundImage(UIImage(),
                                forToolbarPosition: UIBarPosition.Any,
                                barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
                            forToolbarPosition: UIBarPosition.Any)
Colner answered 3/7, 2015 at 6:59 Comment(1)
Sorry if I wasn't clear before. I don't want my toolBar to be transparent. For some reason, the code I provided produces a transparent toolBar.Maidamaidan
S
0

This should disable the translucency/transpacency effect

toolbar.translucent = false
Sausage answered 3/7, 2015 at 8:31 Comment(1)
Thank you for the response! I tried this, but it still has a transparent/translucent effect.Maidamaidan
V
0

Try this

toolBar.barStyle = UIBarStyle.Black

and make sure toolBar.translucent = false

Verney answered 29/6, 2016 at 13:46 Comment(0)
T
0

In Xamarin (similar for swift)

toolbar.Translucent = false;
toolbar.BarTintColor = color;
Topographer answered 25/5, 2018 at 16:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.