Ios 如何模糊分段控件的背景?

Ios 如何模糊分段控件的背景?,ios,swift,iphone,Ios,Swift,Iphone,我正在UITableViewDelegate的表视图(\uU:viewForHeaderInSection:)中定义一个分段控件。因为我使用的是UITableView.Style.plain,所以当表格滚动时,带有分段控件的标题保持在固定位置 我想创建一些上下文,以便未选择的分段控件的背景模糊在其后面滚动的表。以下是ios 13分段控件的外观: 有没有办法模糊分段控件未选定分段的背景 我可以创建这样的分段控件 let items = ["First", "Second"] let custom

我正在UITableViewDelegate的
表视图(\uU:viewForHeaderInSection:)
中定义一个分段控件。因为我使用的是
UITableView.Style.plain
,所以当表格滚动时,带有分段控件的标题保持在固定位置

我想创建一些上下文,以便未选择的分段控件的背景模糊在其后面滚动的表。以下是ios 13分段控件的外观:

有没有办法模糊分段控件未选定分段的背景

我可以创建这样的分段控件

let items = ["First", "Second"]
let customSC = UISegmentedControl(items: items)
customSC.selectedSegmentIndex = 0
我可以用这个创造一个模糊


但我不知道如何把所有的东西都整合起来

这里有一个可能的方法。。。其思想是将模糊效果视图放置在某些包装器视图中分段控件的后面,并将该视图作为表的标题提供

这是一个演示(我的一些设置看起来很不错,但这些都是可配置的),当然gif在这方面不是很好,但可以看到模糊效果是有效的

进近演示代码:

class HeaderView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)

        let blurEffect = UIBlurEffect(style: .dark) // .light looks better as for me, so used in demo
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.translatesAutoresizingMaskIntoConstraints = false

        let items = ["First", "Second"]
        let customSC = UISegmentedControl(items: items)
        customSC.selectedSegmentIndex = 0
        customSC.translatesAutoresizingMaskIntoConstraints = false

        self.addSubview(customSC)
        customSC.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true
        customSC.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        customSC.widthAnchor.constraint(equalToConstant: 200).isActive = true
        customSC.heightAnchor.constraint(equalToConstant: 60).isActive = true

        self.insertSubview(blurEffectView, belowSubview: customSC)
        blurEffectView.leadingAnchor.constraint(equalTo: customSC.leadingAnchor).isActive = true
        blurEffectView.trailingAnchor.constraint(equalTo: customSC.trailingAnchor).isActive = true
        blurEffectView.topAnchor.constraint(equalTo: customSC.topAnchor).isActive = true
        blurEffectView.bottomAnchor.constraint(equalTo: customSC.bottomAnchor).isActive = true
        blurEffectView.layer.cornerRadius = 8
        blurEffectView.layer.masksToBounds = true
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

您可以使用下面的tableView委托创建自定义标题视图

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //Create header view and add blur effect
    let headerView = UITableViewHeaderFooterView()
    let blurEffect = UIBlurEffect(style: .dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    headerView.backgroundView = blurEffectView

    //Add Segmented Control ot headerView
    let items = ["first", "second"]
    let segmentedControl = UISegmentedControl(items: items)
    segmentedControl.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50)
    segmentedControl.backgroundColor = .clear
    segmentedControl.selectedSegmentIndex = 0
    segmentedControl.selectedSegmentTintColor = .red
    headerView.contentView.addSubview(segmentedControl)
    return headerView
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50
}

只是猜测,但可能不是。你在问题中提到iOS 13。。。这是否意味着你可以在iOS 12及更老版本中做到这一点?@dfd-我刚才提到这一点是因为分段控件在iOS 13和iOS 12中看起来不同。我并不是说这只能在以后的版本中完成。如果你想这样做,请检查这个url。你们可以很容易地添加。不,我正在寻找一个模糊效果,以提供桌面视图滚动的上下文
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    //Create header view and add blur effect
    let headerView = UITableViewHeaderFooterView()
    let blurEffect = UIBlurEffect(style: .dark)
    let blurEffectView = UIVisualEffectView(effect: blurEffect)
    headerView.backgroundView = blurEffectView

    //Add Segmented Control ot headerView
    let items = ["first", "second"]
    let segmentedControl = UISegmentedControl(items: items)
    segmentedControl.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 50)
    segmentedControl.backgroundColor = .clear
    segmentedControl.selectedSegmentIndex = 0
    segmentedControl.selectedSegmentTintColor = .red
    headerView.contentView.addSubview(segmentedControl)
    return headerView
}

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 50
}