I have a UIView
which I want to have a radial gradient, and I'm wondering how to go about doing this?
相关问题
- CALayer - backgroundColor flipped?
- 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
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
To do this you need to drop down to Core Graphics and use CGContextDrawRadialGradient.
Similar Stack Overflow Questions
How can I draw a sector with radial gradient (iphone)
How to draw a gradient line (fading in/out) with Core Graphics/iPhone?
Other Resources
There's a tutorial showing how to draw icons with gradients on iOS here:
http://redartisan.com/2011/05/13/porting-iconapp-core-graphics
He's put the code on Github, complete with a UIView subclass that shows the (rather longwinded) way of making gradients using Core Graphics:
https://github.com/crafterm/IconApp/blob/master/IconApp/IconView.m
First subclass a UIView:
And then add it to your view:
Result:
Here's Karlis answer in c# for Xamarin.iOS. Here I am specifying the colors directly but you can of course implement it the same way Karlis did.
Swift 3 - @IBDesignable
I worked off Karlis and Alexander's answers. I aimed to simplify as much as possible. Such as removing color space and locations (
nil
) so the gradient uses the defaults.How to Use
Step 1
Create file and add this code: import UIKit
Step 2
On Storyboard, set the UIView to the above
RadialGradientView
in Identity Inspector:Step 3
Set the Inside Color and Outside Color for your gradient in Attributes Inspector and see the change on your Storyboard:
(Note: I made the UIView on the Storyboard big enough to so it fills the entire
Here's Karlis' answer in Swift 3: