Swift - self.bounds.size.width不返回正确的宽度

时间:2018-03-15 14:03:38

标签: ios swift

在另一个thread中,我找到了分段控件的下划线解决方案。

我的问题的重点似乎是这一点:

let underlineWidth: CGFloat = self.bounds.size.width / CGFloat(self.numberOfSegments)

事实证明,宽度的计算并不完全正确。因为我总是有3个段,所以我希望这个值是屏幕宽度的三分之一(无论设备如何)。

在此屏幕截图中,您可以看到在两个不同设备上运行的应用程序:

Screenshot

正如您所看到的,在iPhone 6S上,下划线的宽度略大,与iPhone 8Plus相比,它太小了。

这只能意味着self.bounds.size.width没有返回正确的宽度。

分段控制的全班:

import UIKit
import Foundation

extension UISegmentedControl{
    func removeBorder(){
        let backgroundImage = UIImage.getColoredRectImageWith(color: UIColor.clear.cgColor, andSize: self.bounds.size)
        let backgroundImageTest = UIImage.getColoredRectImageWith(color: UIColor.red.cgColor, andSize: self.bounds.size)

        self.setBackgroundImage(backgroundImage, for: .normal, barMetrics: .default)
        self.setBackgroundImage(backgroundImageTest, for: .selected, barMetrics: .default)
        self.setBackgroundImage(backgroundImage, for: .highlighted, barMetrics: .default)

        let deviderImage = UIImage.getColoredRectImageWith(color: UIColor.clear.cgColor, andSize: CGSize(width: 1.0, height: self.bounds.size.height))
        self.setDividerImage(deviderImage, forLeftSegmentState: .selected, rightSegmentState: .normal, barMetrics: .default)
        self.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor.gray], for: .normal)
        self.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor(red: 67/255, green: 129/255, blue: 244/255, alpha: 1.0)], for: .selected)
    }

    func addUnderlineForSelectedSegment(){
        removeBorder()
        let underlineWidth: CGFloat = self.bounds.size.width / CGFloat(self.numberOfSegments)
        let underlineHeight: CGFloat = 2.0
        let underlineXPosition = CGFloat(selectedSegmentIndex * Int(underlineWidth))
        let underLineYPosition = self.bounds.size.height - 1.0
        let underlineFrame = CGRect(x: underlineXPosition, y: underLineYPosition, width: underlineWidth, height: underlineHeight)
        let underline = UIView(frame: underlineFrame)
        underline.backgroundColor = UIColor(red: 67/255, green: 129/255, blue: 244/255, alpha: 1.0)
        underline.tag = 1
        self.addSubview(underline)
    }

    func changeUnderlinePosition(){
        guard let underline = self.viewWithTag(1) else {return}
        let underlineFinalXPosition = (self.bounds.width / CGFloat(self.numberOfSegments)) * CGFloat(selectedSegmentIndex)
        UIView.animate(withDuration: 0.1, animations: {
            underline.frame.origin.x = underlineFinalXPosition
        })
    }
}

extension UIImage{

    class func getColoredRectImageWith(color: CGColor, andSize size: CGSize) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
        let graphicsContext = UIGraphicsGetCurrentContext()
        graphicsContext?.setFillColor(color)
        let rectangle = CGRect(x: 0.0, y: 0.0, width: size.width, height: size.height)
        graphicsContext?.fill(rectangle)
        let rectangleImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return rectangleImage!
    }
}

2 个答案:

答案 0 :(得分:1)

您可能正在尝试获取方法func viewDidLoad()中的维度。 尝试采用方法func viewDidLayoutSubviews()中的确切尺寸。

See also this answer

答案 1 :(得分:-1)

视图的确切大小仅在呈现视图后才是正确的。在此之前,视图具有默认大小(例如,在xib或storyboard中)。 在viewDidLayoutSubviews()viewDidAppear() / viewWillAppear()中,大小肯定会正确计算。