0

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.

Community
  • 1
  • 1
Bob F.
  • 93
  • 1
  • 11
  • Remove the "func". This is a call to a method, not the definition of one. – Rob Napier Dec 04 '16 at 22:23
  • Thanks for the reply! If I remove "func", it gives me the error "Use of unresolved identifier 'segmentedControl''. – Bob F. Dec 04 '16 at 22:26
  • See answer below, but if you're having this kind of trouble, you need to stop for a moment and walk through the intro docs, particularly https://developer.apple.com/swift/resources/. Ray Wenderlich also has some very helpful introductions https://www.raywenderlich.com/category/swift. This is very, very basic iOS development, and if you're having trouble here, you're going to fight every step of the way. – Rob Napier Dec 04 '16 at 22:29
  • Yep, I've been working through the Wenderlich tutorials and fighting as I'm working on my project concurrently. I appreciate the guidance! – Bob F. Dec 04 '16 at 22:34
  • (When you said "I created an extension," you gave the impression that you actually wrote the `removeBorders` code rather than simply copying it without credit from a StackOverflow answer (http://stackoverflow.com/a/31102002/97337). That not only is inconsiderate to the person you copied it from, but leads others to answer at a level that doesn't match your needs.) – Rob Napier Dec 04 '16 at 22:35
  • 1
    Fixed! Thanks again – Bob F. Dec 04 '16 at 22:40

1 Answers1

2

Your extension should be placed outside of a any class if it isn't already(looks like it is correct though).

To call a extension function in swift you make sure to have the right type and then just .functionName. Like this:

class AddTaskTableViewController: UITableViewController {
    @IBOutlet weak var segment: UISegmentedControl!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.segment.removeBorders() //segment is of type UISegmentedControl and then just call the function you created in the extension.
    }
}
gasho
  • 1,926
  • 1
  • 16
  • 20
JoakimE
  • 1,820
  • 1
  • 21
  • 30