Hi i'm having trouble to apply gradient color to my UIView
. It is correctly applied, but the color bounds varies with horizontal & portrait orientation.
if I set correctly on Portrait orientation, then the gradient view's bounds are not changed when I flip the device to horizontal.
So, How to get correct constraints and size of the UIView
for both horizontal and portrait device orientation?.
Thanks a lot for your help!!!
Code I have tried:
import UIKit
class ViewController: UIViewController {
@IBOutlet var viewBar: UIView!
override func viewDidLoad() {
super.viewDidLoad()
viewBar.layer.shadowOpacity = 0.5
viewBar.layer.shadowOffset = CGSize(width: 3.0, height:2.0)
viewBar.layer.shadowRadius = 5.0
viewBar.layer.shadowColor = UIColor.blueColor().CGColor
let gradient: CAGradientLayer = CAGradientLayer()
gradient.colors = [UIColor.blueColor().CGColor, UIColor.redColor().CGColor]
gradient.locations = [0.0 , 1.0]
gradient.startPoint = CGPoint(x: 0.0, y: 1.0)
gradient.endPoint = CGPoint(x: 1.0, y: 1.0)
gradient.frame.size = self.viewBar.bounds.size
self.viewBar.layer.insertSublayer(gradient, atIndex: 0)
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Coding output:
In viewDidLoad
the layout haven't happened yet, so you got the wrong frame, move the gradient.frame.size = self.viewBar.bounds.size
to either viewDidLayoutSubviews:
or viewWillAppear:
so that your gradient
gets the correct frame.
override func viewDidLayoutSubviews() {
gradient.frame.size = self.viewBar.bounds.size
}
Check your device orientation in viewDidLayoutSubViews using this method.
+(NSString*)checkDeviceOrientation
{
NSString *deviceOrientation;
if ([[UIDevice currentDevice]orientation]== UIInterfaceOrientationLandscapeLeft ) {
deviceOrientation = @"landscape left mode";
}
else if ([[UIDevice currentDevice]orientation]==UIInterfaceOrientationLandscapeRight){
deviceOrientation = @"landscape right mode";
}
else{
deviceOrientation = @"potrait mode";
}
return deviceOrientation;
}
Then in viewDidLayoutSubviews call this function like this,
-(void)viewDidLayoutSubview
{
if(IS_PHONE4)
if([self.checkDeviceOrientation isEqualtoString;@"landscape right mode"])
{
NSLog(@"Get your view size%@",self.view.frame.size.width);
}
if(IS_IPHONE5)
if([self.checkDeviceOrientation isEqualtoString;@"landscape right mode"])
{
NSLog(@"Get your view size%@",self.view.frame.size.width);
}
.....
}
Also remember to put these constants in any file, you want.
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
#define IS_IPHONE4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0f || IS_IPHONE && [[UIScreen mainScreen]bounds].size.width == 480.0f)
#define IS_IPHONE5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f || IS_IPHONE && [[UIScreen mainScreen]bounds].size.width == 568.0f)
#define IS_IPHONE6 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 667.0 || IS_IPHONE && [[UIScreen mainScreen]bounds].size.width == 667.0f)
#define IS_IPHONE6PLUS (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 736.0 || IS_IPHONE && [[UIScreen mainScreen]bounds].size.width == 736.0f)
#define IS_RETINA ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0))
I am unfamiliar with Swift code but hope, it will help you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With