UIToolBar是透明的

时间:2015-07-03 06:47:12

标签: ios swift uitoolbar

当我添加UIToolBar时,它似乎是透明的。但是,我不希望这种情况发生。这是我的代码:

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

我做错了吗?

5 个答案:

答案 0 :(得分:4)

自己遇到这个问题后,我发现工具栏必须用非零帧进行实例化,或者调用sizeToFit

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

    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

答案 1 :(得分:1)

为UIToolBar Transparent尝试此代码:

self.toolbar.setBackgroundImage(UIImage(),
                                forToolbarPosition: UIBarPosition.Any,
                                barMetrics: UIBarMetrics.Default)
self.toolbar.setShadowImage(UIImage(),
                            forToolbarPosition: UIBarPosition.Any)

答案 2 :(得分:0)

这应该禁用半透明/透明效果

toolbar.translucent = false

答案 3 :(得分:0)

试试这个

toolBar.barStyle = UIBarStyle.Black

并确保toolBar.translucent = false

答案 4 :(得分:-1)

在Xamarin(类似于swift)

toolbar.Translucent = false;
toolbar.BarTintColor = color;