美文网首页
关于swift ios 13出现的选择条适配问题

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

作者: 舟_e9ce | 来源:发表于2019-10-11 16:19 被阅读0次

问题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)
        }
    }

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

相关文章

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

    问题view 正常view(无动画) 问题:在ios 13中自适应适配时出现点击后滚动条出现中心点为0的情况,在1...

  • iOS 关于iOS13那些事

    本文记录一些关于iOS 13的内容,欢迎指正和补充! 一、关于iOS13适配 1.关于一些私有属性的适配,iOS ...

  • 2019-12-03

    iOS 13适配:UITabBarItem上title颜色变成蓝色的问题 适配iOS 13发现设置UITabBar...

  • iOS13 适配问题 看这一篇就够了

    技术参考: apple login IOS13适配-详细 iOS 13 适配(持续更新中) iOS13适配 掘金 ...

  • iOS 13适配

    技术参考: apple login IOS13适配-详细 iOS 13 适配(持续更新中) iOS13适配 掘金 ...

  • iOS 13 适配要点总结(转)

    转自: iOS 13 适配要点总结 iOS 13适配

  • iOS13适配

    参考: iOS13 适配踩坑 - 持续更新 iOS 13 适配要点总结 iOS 13 适配要点总结 1、prese...

  • 暗黑模式开发

    iOS13暗黑模式适配(项目开发版) iOS 13 DarkMode 暗黑模式 IOS 暗黑模式适配---基础适配

  • 2019-10-08

    iOS13适配:1、presentViewController的问题会出现这种情况是主要是因为我们之前对UIVie...

  • iOS开发 - iOS15导航栏适配(Object-C、Swif

    iOS15导航栏适配 设置导航栏纯色/透明、解决ScrollView类上滑导航栏出现磨砂阴影的问题 Swift版导...

网友评论

      本文标题:关于swift ios 13出现的选择条适配问题

      本文链接:https://www.haomeiwen.com/subject/nxxxmctx.html