I created an extension to remove the borders from a Segmented Control. This is the extension, titled "UISegmentedControl+removeborders.swift":
import UIKit
extension UISegmentedControl {
func removeBorders() {
setBackgroundImage(imageWithColor(backgroundColor!), forState: .Normal, barMetrics: .Default)
setBackgroundImage(imageWithColor(tintColor!), forState: .Selected, barMetrics: .Default)
setDividerImage(imageWithColor(UIColor.clearColor()), forLeftSegmentState: .Normal, rightSegmentState: .Normal, barMetrics: .Default)
}
private func imageWithColor(color: UIColor) -> UIImage {
let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor);
CGContextFillRect(context, rect);
let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image
}
}
Xcode didn't pick up any errors in that code, but I'm having a hard time figuring out how to call the extension. This is the jist of the code for the Table View Controller that the Segmented Control is in, titled "AddTaskTableViewController.swift":
import UIKit
class AddTaskTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
func segmentedControl.removeBorders()
}
}
I'm trying to call the extension with that last line "func segmentedControl.removeBorders()", but I'm getting two errors on that line: 1) Consecutive statements on a line must be separated by ';' 2) Expected '(' in argument list of function declaration. Admittedly I'm a n00b of the worst sort, which is why I'm hung up on this. What should that last line of code be, or should I be calling the extension a different way? Thank you!!!
Note: The code I'm using for my extension is from the 3rd response in Remove UISegmentedControl separators completely. (iphone) if that helps you understand how such a n00b could have come up with such a thing.