Misplaced CAGradientLayer on iPhone 6s


I have a few UILabels with colored background and I added CAGradientLayers to them like below.

CAGradientLayer * gradientLayer = [CAGradientLayer layer]; gradientLayer.colors = [NSArray arrayWithObjects: (id) [UIColor colorWithWhite:1 alpha:0.3].CGColor, (id) [UIColor colorWithWhite:0.5 alpha:0.3].CGColor, nil]; gradientLayer.frame = label.bounds; gradientLayer.cornerRadius = label.layer.cornerRadius; [label.layer insertSublayer:gradientLayer atIndex:0];

On iPhone 4 and iPhone 5s it looks perfect, but on iPhone 6s the CAGradientLayers are misplaced horizontally like the following screenshot.

<a href="https://i.stack.imgur.com/k9W1h.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/k9W1h.png" data-original="https://i.stack.imgur.com/k9W1h.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

This problem happens for UIButtons as well.

I am running iOS 9.3.


What you need is deal with autolayout constraints. This swift code represents what you need to do at viewDidLayoutSubviews method:

class ViewController: UIViewController { @IBOutlet weak var button: UIButton! let gradientLayer = CAGradientLayer() override func viewDidLoad() { super.viewDidLoad() gradientLayer.colors = [UIColor(white: 1, alpha: 0.3).CGColor, UIColor(white: 0.5, alpha: 0.3).CGColor] gradientLayer.frame = button.bounds gradientLayer.cornerRadius = button.layer.cornerRadius button.layer.insertSublayer(gradientLayer, atIndex: 0) } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() gradientLayer.frame = button.bounds } }


  • How to return response from cognito async function and display it on the page?
  • MOQ'ing method call sequence
  • Spark toDF() / createDataFrame() type inference doesn't work as expected
  • Question on XPATH for an XSLT File And XSLT If Statement
  • Controlling the behavior of the Browsers Back Button
  • Get a class as javax.lang.model.element.Element in junit tests
  • Don't understand rounding behavior in sql server when using division operator
  • WPF DataGrid with TreeView type hierarchy
  • Google API Level 18 emulator “This app won't run unless you update Google Play services”
  • Attach an event handler to a StopWatch
  • Upload document to specific folder in a SharePoint Document library using WebClient
  • Dropbox API: how can I get the amount of remaining free space?
  • Append extra row to jqueryui autocomplete (even if autocomplete doesn't have any matches)
  • On Woocommerce order complete, activate woosensei course
  • Downgrade NG2 Directive to AngularJS
  • Debug Azure Functions locally
  • Zip library for Windows Phone 7
  • Is there a way to convert all existing table data to UTF8 collation?
  • Pass multiple lines of stdin input to interactive Java command line program, non-interactively
  • How to HTTP POST a Blob to a different domain? [duplicate]
  • Faces Servlet not parsing .xhtml pages in jsf 2. running on tomcat 7
  • Web scraping pdf files from HTML
  • connect.cookieParser and connect.session
  • Using django-multiupload within a ModelForm
  • Javascript / jQuery not executing in IE until the body of the page is moused over
  • MFMailComposer send email without presenting view
  • Find corners of a rotated rectangle
  • Validating my form with Jquery
  • Symfony - Setting Cookie onKernelRequest
  • Query takes almost two seconds but matches only two rows - why isn't the index helping?
  • Can a PHP script be scheduled to run at a specific time or after a specific amount of time has expir
  • php “page caching” solution suggestions for CMS Applications
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?