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

    kozer

    Joined:
    Aug 8, 2016
    #1
    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.
    Code:
    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
    Code:
    [self.centerCollectionView reloadData];
    
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    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

    http://stackoverflow.com/questions/24303279/auto-layout-constraint-on-calayer-ios
    http://stackoverflow.com/questions/15581374/how-to-resize-a-cagradientlayer-after-rotation
    https://recalll.co/app/?q=ios - Issue adding CAGradientLayer sublayer to UIView

    and many other comments.
     

Share This Page