简体   繁体   English

将 UISegmentedControl 的颜色与半透明导航栏的颜色匹配

[英]Match UISegmentedControl's color with that of translucent navigation bar

I am adding a segmented control directly under the (translucent) navigation bar, and want it to have the same color.我正在(半透明)导航栏下直接添加一个分段控件,并希望它具有相同的颜色。 I am trying to make it look like it's part of the navigation bar.我试图让它看起来像是导航栏的一部分。

isOpaque = false
layer.opacity = 0.85
let bkImg = UIImage(color: .systemBackground.withAplhaComponent(0.85), size: CGSize(width: 1, height: 16))
segmentedCtrl.setBackgroundImage(bkImg, for: .normal, barMetrics: .default)
setDividerImage(bkImg, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)

I've tried playing with the opacity of the segmented control, and the color of bkImg , but that has no effect at all.我尝试过使用分段控件的不透明度和bkImg的颜色,但这根本没有效果。 It would have been quicker to just write a custom tab bar from scratch - the segmented control in iOS 13 is impossible to customize.从头开始编写自定义标签栏会更快 - iOS 13 中的分段控件无法自定义。

This is a total hack, but it looks good.这是一个完全的黑客,但它看起来不错。

在此处输入图像描述

class CustomSegmentedControl: UISegmentedControl {
    private var underline: UIView!

    init() {
        super.init(frame: .zero)

        underline = UIView()
        underline.backgroundColor = tintColor
        view.addSubvie(underline)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        // Set background color to match navigation bar
        let bgColor = UIColor.systemBackground.hsbComponents.brightness == 1.0
            ? UIColor.black.withAlphaComponent(0.028) // light
            : UIColor.white.withAlphaComponent(0.073) // dark
        let img = UIImage(color: bgColor, size: CGSize(width: 1, height: 16))
        setBackgroundImage(img, for: .normal, barMetrics: .default)
        setDividerImage(img, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)

        // Bonus: underline below selected tab
        var frame = underline.frame
        let tabWidth = self.frame.width / CGFloat(numberOfSegments)
        frame.origin.x = CGFloat(selectedSegmentIndex) * tabWidth
        frame.origin.y = self.frame.height - 2.0
        frame.size.width = tabWidth
        underline.frame = frame
    }
}

This effect can be accomplished by putting the UISegmentedControl inside of a UIToolbar , and positioning that UIToolbar beneath the UINavigationBar .这种效果可以通过将UISegmentedControl放在UIToolbar中,并将UIToolbar放置在UINavigationBar下方来实现。 Once inside of a UIToolbar , the UISegmentedControl takes on the same appearance that it has in a UINavigationBar .一旦进入UIToolbarUISegmentedControl就会呈现出与UINavigationBar相同的外观。

If you want the UIToolBar to blend into the UINavigationBar , you will need to apply all of the same appearance options to the UIToolBar that you do to the UINavigationBar (eg barTintColor and tintColor ).如果您希望UIToolBar融入UINavigationBar ,您需要将所有相同的外观选项应用于UIToolBar ,就像您对UINavigationBar所做的一样(例如barTintColortintColor )。

I do not know how to tweak the UISegmentedControl itself to mimic the appearance that the navigation bar and toolbar apply, but with this strategy we do not need to.我不知道如何调整UISegmentedControl本身以模仿导航栏和工具栏应用的外观,但使用这种策略我们不需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM