I'm trying to generate a view with a gradient color background (A solid color to transparent) at runtime. Is there a way of doing that?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
- Attempt to present UIAlertController on View Contr
In Swift 3.1 I have added this extension to UIView
which I then call with
You can create a custom class
GradientView
:Swift 4, Swift 3
In the storyboard, set the class type to any view that you want to have gradient background:
This is better in the following ways:
CLayer
NSConstraint
as usual on theUIView
Swift 4:
Shows gradient in IB correctly:
This is my recommended approach.
To promote reusability, I'd say create a category of
CAGradientLayer
and add your desired gradients as class methods. Specify them in theheader
file like this :Then in your implementation file, specify each gradient with this syntax :
Then simply import this category in your
ViewController
or any other requiredsubclass
, and use it like this :Swift Implementation:
Simple swift view based on Yuchen's version
Then you can use gradientLayer after initialization like this...