SLPagingViewSwift
SLPagingViewSwift copied to clipboard
SLPagingView within UIViewController
Hi,
Thanks for you repository. I need to add it to on of my viewController. I don't need it at AppDelegate.
This is my try:
class PagingViewController: SLPagingViewController {
override init(navBarItems items: [AnyObject]!, controllers: [AnyObject]!, showPageControl addPageControl: Bool) {
var orange = UIColor(red: 255/255, green: 69.0/255, blue: 0.0/255, alpha: 1.0)
var gray = UIColor(red: 0.84, green: 0.84, blue: 0.84, alpha: 1.0)
var ctr1 = UIViewController()
ctr1.title = "Ctr1"
ctr1.view.backgroundColor = orange
var ctr2 = UIViewController()
ctr2.title = "Ctr2"
ctr2.view.backgroundColor = UIColor.yellowColor()
var ctr3 = UIViewController()
ctr3.title = "Ctr3"
ctr3.view.backgroundColor = gray
var img1 = UIImage(named: "heart_selected")
img1 = img1?.imageWithRenderingMode(.AlwaysTemplate)
var img2 = UIImage(named: "people")
img2 = img2?.imageWithRenderingMode(.AlwaysTemplate)
var img3 = UIImage(named: "comment")
img3 = img3?.imageWithRenderingMode(.AlwaysTemplate)
var items = [UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)]
var controllers = [ctr1, ctr2, ctr3]
super.init(navBarItems: items, controllers: controllers, showPageControl: false)
self.pagingViewMoving = ({ subviews in
for v in subviews {
var lbl = v as! UIImageView
var c = gray
if(lbl.frame.origin.x > 45 && lbl.frame.origin.x < 145) {
c = self.gradient(Double(lbl.frame.origin.x), topX: Double(46), bottomX: Double(144), initC: orange, goal: gray)
}
else if (lbl.frame.origin.x > 145 && lbl.frame.origin.x < 245) {
c = self.gradient(Double(lbl.frame.origin.x), topX: Double(146), bottomX: Double(244), initC: gray, goal: orange)
}
else if(lbl.frame.origin.x == 145){
c = orange
}
lbl.tintColor = c
}
})
}
required init(coder aDecoder: NSCoder)
{
super.init(coder: aDecoder)
}
func gradient(percent: Double, topX: Double, bottomX: Double, initC: UIColor, goal: UIColor) -> UIColor{
var t = (percent - bottomX) / (topX - bottomX)
let cgInit = CGColorGetComponents(initC.CGColor)
let cgGoal = CGColorGetComponents(goal.CGColor)
var r = cgInit[0] + CGFloat(t) * (cgGoal[0] - cgInit[0])
var g = cgInit[1] + CGFloat(t) * (cgGoal[1] - cgInit[1])
var b = cgInit[2] + CGFloat(t) * (cgGoal[2] - cgInit[2])
return UIColor(red: r, green: g, blue: b, alpha: 1.0)
}
}
Could you please guide me to the correct way to achieve it?!
Hello,
I think the point is to have it in the AppDelegate and then swap out the
var ctr1 = UIViewController()
ctr1.title = "Ctr1"
ctr1.view.backgroundColor = orange
var ctr2 = UIViewController()
ctr2.title = "Ctr2"
ctr2.view.backgroundColor = UIColor.yellowColor()
var ctr3 = UIViewController()
ctr3.title = "Ctr3"
ctr3.view.backgroundColor = gray
Replace the UIViewController() with your view. What's the bigger picture of your app?
Yaroslaw Bagriy
Hi Yaroslaw Bagriy thanks for your reply. I need it in one of my viewcontrollers I know how to do it in Appdelegate, but I can't find a way to do it in one viewcontroller. Do you know a way to achieve it?
Hello,
Yes it is possible. So make a new class that subclasses UIViewController. Then make a storyboard UIViewController and set the class to the new UIViewController class you made.
In viewDidLoad add
var controller: SLPagingViewSwift!
var ctr1 = TestButtonViewController()
ctr1.title = "Ctr1"
var ctr2 = TestButtonViewController()
ctr2.title = "Ctr2"
var ctr3 = TestButtonViewController()
ctr3.title = "Ctr3"
var img1 = UIImage(named: "gear")
img1 = img1?.imageWithRenderingMode(.AlwaysTemplate)
var img2 = UIImage(named: "profile")
img2 = img2?.imageWithRenderingMode(.AlwaysTemplate)
var img3 = UIImage(named: "chat")
img3 = img3?.imageWithRenderingMode(.AlwaysTemplate)
var items = [UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)]
var controllers = [ctr1, ctr2, ctr3]
controller = SLPagingViewSwift(items: items, controllers: controllers, showPageControl: false)
controller.pagingViewMoving = ({ subviews in
for lbl in (subviews as! [UIImageView]) {
var c : UIColor!
switch (lbl.frame.origin.x) {
case 145:
c = orange
case 46 ... 144:
c = self.gradient(Double(lbl.frame.origin.x), topX: Double(46), bottomX: Double(144), initC: orange, goal: gray)
case 146 ... 244:
c = self.gradient(Double(lbl.frame.origin.x), topX: Double(146), bottomX: Double(244), initC: gray, goal: orange)
default:
c = gray
}
lbl.tintColor = c
}
})
self.presentviewcontroller(controller)
See if that works.
Yaroslaw Bagriy
Thanks again. First Swift version has many errors so I'm using objective c version. I tried your code I got two errors: 'SLPagingViewController' does not have a member named 'pagingViewMoving'
'TryViewController' does not have a member named 'presentviewcontroller'
var controller: SLPagingViewController! var ctr1 = Try0ViewController() ctr1.title = "Ctr1" var ctr2 = Try1ViewController() ctr2.title = "Ctr2" var ctr3 = Try2ViewController() ctr3.title = "Ctr3"
var img1 = UIImage(named: "heart_selected")
img1 = img1?.imageWithRenderingMode(.AlwaysTemplate)
var img2 = UIImage(named: "people")
img2 = img2?.imageWithRenderingMode(.AlwaysTemplate)
var img3 = UIImage(named: "comment")
img3 = img3?.imageWithRenderingMode(.AlwaysTemplate)
var items = [UIImageView(image: img1), UIImageView(image: img2), UIImageView(image: img3)]
var controllers = [ctr1, ctr2, ctr3]
controller = SLPagingViewController(navBarItems: items, controllers: controllers, showPageControl: false)
controller.pagingViewMoving = ({ subviews in
for lbl in (subviews as! [UIImageView]) {
var c : UIColor!
switch (lbl.frame.origin.x) {
case 145:
c = orange
case 46 ... 144:
c = self.gradient(Double(lbl.frame.origin.x), topX: Double(46), bottomX: Double(144), initC: orange, goal: gray)
case 146 ... 244:
c = self.gradient(Double(lbl.frame.origin.x), topX: Double(146), bottomX: Double(244), initC: gray, goal: orange)
default:
c = gray
}
lbl.tintColor = c
}
})
self.presentviewcontroller(controller)
Any idea how to solve it?
Opps wrong parameters for presentviewcontroller. try
self.presentViewController(controller, animated: false, completion: nil)
Yup I tried this but it doesn't work. So I used another repository and it works perfectly with me:
https://github.com/Tgy31/THTinderNavigationController
Thanks for your help.
I was able to get the swiping function to work but the issue that I'm facing is the header doesn't appear.