Created
November 16, 2018 10:11
-
-
Save Briahas/38bb7d6b90b4c16a5160f1628d4b8003 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Изменение размеров CALayer'a лежит полностью на его пользователе. | |
Поэтому вам нужно при каждом изменении frame у вью, менять положиние / размер слоя | |
Пример: | |
class MyView: UIView { | |
let gradient: CALayer | |
... | |
override func layoutSubviews() { | |
super.layoutSubviews() | |
gradient.frame = self.bounds | |
} | |
} | |
Если прям очень хочется через экстеншн, то примерно так: | |
private var gradientPropertyKey: UInt8 = 0 | |
extension UIView { | |
func addNiceGradient() { | |
let gradient = < тут ваш слой > | |
layer.addSublayer(gradient) | |
let observation = layer.observe(\.bounds) { [weak gradient] layer, _ in | |
gradient?.frame = layer.bounds | |
} | |
objc_setAssociatedObject(self, &gradientPropertyKey, observation, .OBJC_ASSOCIATION_RETAIN) | |
} | |
} | |
Но будет работать хуже, да и уверены что хотите в ObjC Runtime без причины лезть? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment