title: Swift - self.bounds.size.width doesn't return the correct width
body: <p>In another <a href="https://stackoverflow.com/a/42760021/9472937">thread</a> I found a solution for an underline for a <em>segmented control</em>. </p>
  
  <p>The important line for my problem seems to be this one:</p>
  
  <pre><code>let underlineWidth: CGFloat = self.bounds.size.width / CGFloat(self.numberOfSegments)
  </code></pre>
  
  <p>It turns out that the calculation of the width is not entirely correct. Since I have always 3 segments I expected this value to be a third of the screen width (regardless of the device).</p>
  
  <p>On this screenshot you can see the application running on two different devices:</p>
  
  <p><a href="https://i.stack.imgur.com/k52eB.png" rel="nofollow noreferrer">Screenshot</a></p>
  
  <p>As you can see, on the iPhone 6S, the width of the underline is slightly too big, where as on the iPhone 8Plus it's too small.</p>
  
  <p>That can only mean that self.bounds.size.width doesn't return the correct width.</p>
  
  <hr>
  
  <p>The whole class for the segmented control:</p>
  
  <pre><code>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) -&gt; 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!
      }
  }
  </code></pre>