-
Notifications
You must be signed in to change notification settings - Fork 0
SegmentedControl with View
// SegmentedControl with View
import Foundation import UIKit
class MasterViewController: UIViewController { let containerViewControl = UIView() let containerViewA = UIView() let containerViewB = UIView() let containerViewC = UIView()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .green
setupSegmentedControl()
containerViewA.frame = CGRect(x: 20, y: 20, width: self.view.frame.size.width - 40, height: self.view.frame.size.height)
containerViewA.backgroundColor = .yellow
containerViewControl.addSubview(containerViewA)
containerViewB.frame = CGRect(x: 30, y: 40, width: self.view.frame.size.width - 60, height: self.view.frame.size.height)
containerViewB.backgroundColor = .gray
containerViewControl.addSubview(containerViewB)
containerViewC.frame = CGRect(x: 40, y: 60, width: self.view.frame.size.width - 60, height: self.view.frame.size.height)
containerViewC.backgroundColor = .orange
containerViewControl.addSubview(containerViewC)
containerViewControl.frame = CGRect(x: 10, y: 120, width: self.view.frame.size.width - 20, height: self.view.frame.size.height)
containerViewControl.backgroundColor = .purple
view.addSubview(containerViewControl)
}
func setupSegmentedControl(){
let mySegmentedControl = UISegmentedControl (items: ["One","Two","Three"])
let xPostion:CGFloat = 10
let yPostion:CGFloat = 80
let elementWidth:CGFloat = 300
let elementHeight:CGFloat = 30
mySegmentedControl.frame = CGRect(x: xPostion, y: yPostion, width: elementWidth, height: elementHeight)
// Make second segment selected
mySegmentedControl.selectedSegmentIndex = 2
//Change text color of UISegmentedControl
mySegmentedControl.tintColor = UIColor.orange
//Change UISegmentedControl background colour
mySegmentedControl.backgroundColor = UIColor.white
// Add function to handle Value Changed events
mySegmentedControl.addTarget(self, action: #selector(self.segmentedValueChanged(_:)), for: .valueChanged)
self.view.addSubview(mySegmentedControl)
}
func segmentedValueChanged(_ sender:UISegmentedControl!)
{
print("Selected Segment Index is : \(sender.selectedSegmentIndex)")
// if sender.selectedSegmentIndex == 0 {
//
// //navigationController?.pushViewController(GreenViewController(), animated: true)
// } else if sender.selectedSegmentIndex == 1 {
//
// // navigationController?.pushViewController(YellowViewController(), animated: true)
// } else if sender.selectedSegmentIndex == 2 {
// // navigationController?.pushViewController(PurpleViewController(), animated: true)
// }
if sender.selectedSegmentIndex == 0 {
UIView.animate(withDuration: 0.5, animations: {
// self.containerViewA.alpha = 1 // self.containerViewB.alpha = 0 // self.containerViewC.alpha = 0 self.containerViewA.isHidden = false self.containerViewB.isHidden = true self.containerViewC.isHidden = true
})
} else if sender.selectedSegmentIndex == 1 {
UIView.animate(withDuration: 0.5, animations: {
// self.containerViewA.alpha = 0 // self.containerViewB.alpha = 1 // self.containerViewC.alpha = 0
self.containerViewA.isHidden = true
self.containerViewB.isHidden = false
self.containerViewC.isHidden = true
})
} else if sender.selectedSegmentIndex == 2{
UIView.animate(withDuration: 0.5, animations: {
// self.containerViewA.alpha = 0 // self.containerViewB.alpha = 0 // self.containerViewC.alpha = 1
self.containerViewA.isHidden = true
self.containerViewB.isHidden = true
self.containerViewC.isHidden = false
})
}
}
}