swift - iOS8: Auto-layout and Gradient -
setup:
i have view controller
consists of view
, container view
.
the view
(orange) pinned
top
0, left
0, , right
0.
the container view
(gray) pinned
bottom
0, left
0, , right
0.
the view
's bottom space to: container view
= 0
the view
's proportional height container
view = 1
desired results: add gradient background of view
(orange)
tried:
i'm using auto-layout class sizes different behavior on different screen.
code:
class viewcontroller: uiviewcontroller { @iboutlet weak var graphview: uiview! @iboutlet weak var containerview: uiview! override func viewdidload() { super.viewdidload() let backgroundcolor = cagradientlayer().graphviewbackgroundcolor() backgroundcolor.frame = self.graphview.frame self.graphview.layer.addsublayer(backgroundcolor) }
i have category:
extension cagradientlayer { func graphviewbackgroundcolor() -> cagradientlayer { let topcolor = uicolor(red: (160/255.0), green: (160/255.0), blue: (160/255.0), alpha: 1) let bottomcolor = uicolor(red: (52/255.0), green: (53/255.0), blue: (52/255.0), alpha: 1) let gradientcolors: [cgcolor] = [topcolor.cgcolor, bottomcolor.cgcolor] let gradientlocations: [float] = [0.0, 1.0] let gradientlayer: cagradientlayer = cagradientlayer() gradientlayer.colors = gradientcolors gradientlayer.locations = gradientlocations return gradientlayer } }
result:
as can see gradient did not cover entire view
.
question: how can gradient cover entire view
update:
when place code in viewdidlayoutsubviews()
looks weird when rotate:
simply inside viewdidlayoutsubviews:
override func viewdidlayoutsubview() { super.viewdidlayoutsubviews backgroundcolor.frame = self.graphview.bounds }
viewdidlayoutsubviews should called when rotate device.
if not called, override method , as,
override func viewwilltransitiontosize(size: cgsize, withtransitioncoordinator coordinator: uiviewcontrollertransitioncoordinator) { super.viewwilltransitiontosize(size, withtransitioncoordinator: coordinator) backgroundcolor.frame = self.graphview.bounds }
Comments
Post a Comment