ios gradient background after constraint applied

Discussion in 'iOS Programming' started by kozer, Aug 8, 2016.

  1. kozer, Aug 8, 2016
    Last edited by a moderator: Aug 8, 2016

    kozer macrumors newbie


    Aug 8, 2016
    I have a xib file which has a contain one uiview and 3 uibutton. flush-seamed spaced.(uiview 2px space uibuttun 2px space ...)

    I apply constraints in xib files and they are automatically right place if device ipad,iphone or landscape or portrait first launching. They are getting bigger or smaller according to device. the problem start when i apply gradient to buttons.
    CAGradientLayer*gradientMain =[CAGradientLayer layer];
    gradientMain.frame = myview.frame;//myview.bounds no matter
    gradientMain.colors =[NSArray arrayWithObjects:(id)[[UIColor colorWithRed:(65/255.0) green:(64/255.0) blue:(66/255.0) alpha:1.0]CGColor],(id)[[UIColor colorWithRed:(35/255.0)  green:(31/255.0)  blue:(32/255.0)  alpha:1.0]CGColor],nil];
    [myview.layer insertSublayer:gradientMain atIndex:0];
    afterthis code, when i change orientation of device bounds in xib file sizes and views are conflicting. How can i achive this problem.

    İ try to these after orientation, but not work
    [self.centerCollectionView reloadData];
  2. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    I haven't messed with layers too much but Mr Google tells me that this is a common issue. The view doesn't keep its gradientLayer's frame in sync. You have to add code to do that. One way is to subclass UIView and implement layoutSubviews and set the gradient layer's frame to self.bounds. You can set the gradient layer's frame in the view controller also if you wish.

    See - Issue adding CAGradientLayer sublayer to UIView

    and many other comments.

Share This Page