关于swift ios 13出现的选择条适配问题

2019-10-11  本文已影响0人  舟_e9ce

问题view


曹竹村 2019-10-10 16.19.59.gif

正常view(无动画)


清晨 2019-10-10 16.26.35.gif

问题:
在ios 13中自适应适配时出现点击后滚动条出现中心点为0的情况,在13以下不会出现。
解决办法:
手动设置view的frame

问题代码(iOS 13 中会出现下面的滚动条只会显示在第一个label下面)

override func layoutSubviews() {
        super.layoutSubviews()
        bottomLine.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview().offset(0)
            make.height.equalTo(1)
        }

        for (index,label) in labelArray.enumerated() {
            label.snp.makeConstraints { (make) in
                if index == 0 {
                    make.left.equalToSuperview().offset(0)
                } else {
                    make.left.equalTo(labelArray[index - 1].snp.right).offset(0)
                }
                make.top.equalToSuperview().offset(0)
                make.bottom.equalTo(bottomLine.snp.top).offset(0)
                make.width.equalTo(titleW)
            }
        }
        if labelArray.count > 0 {
            scrollLine.snp.makeConstraints { (make) in
                make.bottom.equalTo(bottomLine.snp.top).offset(-2)
                make.height.equalTo(2)
                make.centerX.equalTo(labelArray[currentLabel].snp.centerX)
                make.width.equalTo(titleArray[currentLabel].get_widthForComment(fontSize: 14, height: 20))
            }
        }

    }

手动设置frame(这个不需要提前知道父视图的frame),在改变滚动条大小时,会在走此方法,不过感觉不碍事。

override func layoutSubviews() {
        super.layoutSubviews()
        
        bottomLine.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview().offset(0)
            make.height.equalTo(1)
        }

        for (index,label) in labelArray.enumerated() {
            if index == 0 {
                label.frame = CGRect(x: 0, y: 0, width: titleW, height: self.frame.size.height)
            } else {
                label.frame = CGRect(x: CGFloat(index) * titleW, y: 0, width: titleW, height: self.frame.size.height)
            }
        }
        let label = labelArray[currentLabel]
        let tempW = titleArray[label.tag].get_widthForComment(fontSize: 14, height: label.frame.size.height)
        scrollLine.center = CGPoint(x: label.center.x, y: label.frame.size.height - 4)
        scrollLine.bounds = CGRect(x: 0, y: 0, width: tempW, height: 2)
    }

如果在设置label时就设置frame,需要提前知道父视图的frame

for (index,title) in titleArray.enumerated() {
            let label = UILabel()
            label.font = UIFont.systemFont(ofSize: 14)
            label.text = title
            label.textAlignment = .center
            label.isUserInteractionEnabled = true
            label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(touchLabelMethod)))
            label.tag = index
            if index == 0 {
                let tempW = titleArray[label.tag].get_widthForComment(fontSize: 14, height: label.frame.size.height)
                label.frame = CGRect(x: 0, y: 0, width: titleW, height: self.frame.size.height)
                scrollLine.center = CGPoint(x: label.center.x, y: label.frame.size.height - 4)
                scrollLine.bounds = CGRect(x: 0, y: 0, width: tempW, height: 2)
            } else {
                label.frame = CGRect(x: CGFloat(index) * titleW, y: 0, width: titleW, height: self.frame.size.height)
            }
            self.addSubview(label)
            labelArray.append(label)
        }

完整代码(简单的一个选择工具)

func setupNormalLabel(titles: [String]) {
        labelArray.removeAll()
        self.titleArray = titles
        titleW = kScrenW / CGFloat(titles.count)
        bottomLine.snp.makeConstraints { (make) in
            make.left.bottom.right.equalToSuperview().offset(0)
            make.height.equalTo(1)
        }
        for (index,title) in titleArray.enumerated() {
            let label = UILabel()
            label.font = UIFont.systemFont(ofSize: 14)
            label.text = title
            label.textAlignment = .center
            label.isUserInteractionEnabled = true
            label.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(touchLabelMethod)))
            label.tag = index
            if index == 0 {
                let tempW = titleArray[label.tag].get_widthForComment(fontSize: 14, height: label.frame.size.height)
                label.frame = CGRect(x: 0, y: 0, width: titleW, height: self.frame.size.height)
                scrollLine.center = CGPoint(x: label.center.x, y: label.frame.size.height - 4)
                scrollLine.bounds = CGRect(x: 0, y: 0, width: tempW, height: 2)
            } else {
                label.frame = CGRect(x: CGFloat(index) * titleW, y: 0, width: titleW, height: self.frame.size.height)
            }
            self.addSubview(label)
            labelArray.append(label)
        }
    }
    
    @objc func touchLabelMethod(tapGestureRecognizer:UITapGestureRecognizer) {
        
        let label = tapGestureRecognizer.view!
        if currentLabel == label.tag  {
            return
        }
        currentLabel = label.tag
        print(label.tag)
        let tempW = titleArray[label.tag].get_widthForComment(fontSize: 14, height: label.frame.size.height)
        scrollLine.bounds = CGRect(x: 0, y: 0, width: tempW, height: 2)
        scrollLine.center = CGPoint(x: label.center.x, y: scrollLine.center.y)
        if self.delegate != nil && self.delegate.responds(to: #selector(self.delegate.tabbarEqualTouchView(view:touchIndex:))) {
            self.delegate.tabbarEqualTouchView(view: self, touchIndex: currentLabel)
        }
    }

如有更好的解决方法,请各路大神指教!

上一篇下一篇

猜你喜欢

热点阅读