I am new to iOS development and I am working with the UIStackView.
When I use StoryBoard to create two UILabels as child objects of UIStackView the following code to make gradient backround color of each UILabel works fine.
UILabel *label;
for (int ii = 1; ii < 3; ii++) {
label = [self.view viewWithTag:ii];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = label.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor yellowColor]CGColor], (id)[[UIColor grayColor]CGColor], nil];
[label.layer insertSublayer:gradient atIndex:0];
UILabel *newLabel = [[UILabel alloc] initWithFrame:label.bounds];
newLabel.backgroundColor = [UIColor clearColor];
newLabel.text = label.text;
[label addSubview:newLabel];
}
But when I create two UILabels programatically with using the code bellow,
my code above for making gradient background of each UILabel doesn't work and backround color stays white.
Can someone help me with this?
UILabel *myLabel;
myLabel = [[UILabel alloc] init];
[myLabel setText:@"text1"];
[myLabel setTag:1];
[stackView addArrangedSubview:myLabel];
myLabel = [[UILabel alloc] init];
[myLabel setText:@"text2"];
[myLabel setTag:2];
[stackView addArrangedSubview:myLabel];