I'm extremely new to iOS development and ran into some trouble while building an app for a course.
I created a segmented control and its init function (shown below) is being called in the view controller class containing the segmented control. I was able to remove all borders and dividers of the segmented control from the segmented control class as follows:
import Foundation
import UIKit
class CashSegmentedControl: UISegmentedControl{
func initUI(){
removeBorders()
}
func removeBorders(){
self.tintColor = UIColor.clear
}
I want it to have a line under each segment WHEN the segment is selected (similar to instagram)
I've searched a lot and come across some posts on StackOverflow but they seem to be for older versions of Swift. I'd really appreciate any help in this matter, and if there is a better solution for customising the borders (other than what I have done), I'd love to learn more!
Thanks a lot :)
A.Jam's answer in Xcode 9.3, Swift 4.2 version
you can use this
Add the following code in a separate swift file (command+N -> New File):
Then after call
segmentedControl.addUnderlineForSelectedSegment()
from yourviewDidLoad()
method, and create an@IBAction
method for the segmented control like so:Then call
segmentedControl.changeUnderlinePosition()
from within this method.Do not forget to connect the segmented control from your storyboard to the
@IBAction
method you just created.Very important: Don't forget to use Auto layout in the storyboard to determine the size and position of your segmented control.
This is the result:
Feel free to ask any other questions you may have :)