简体   繁体   中英

Programmatically added subview frames don't show up right in scrollview

I'm creating a scrollView programmatically, and all the views in it. However, when I add more then 1 view, the other views layers don't show up right in their respective frames.

For example:

-(void)viewDidLoad {
  [super viewDidLoad];

  self.scrollView = [[UIScrollView alloc] init];
  self.scrollView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
  self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width, 150*5);
  self.scrollView.backgroundColor = [UIColor lightGrayColor];
  self.scrollView.scrollEnabled = YES;
  self.scrollView.showsVerticalScrollIndicator = NO;
  [self.view addSubview:self.scrollView];

  [self setUpLogInButtons];
}

Then I set the subViews to scrollView up here:

-(void)setUpLogInButtons {
  CGFloat subviewWidth = self.scrollView.frame.size.width/2;
  CGFloat subviewHeight = 150;

  UIView *facebookView = [[UIView alloc] init];
  facebookView.frame = CGRectMake(0, 0, subviewWidth, subviewHeight);
  [facebookView.layer insertSublayer:[self gradientLayerForViewFrame:facebookView.frame withColors:[self colorArrayWithValues:0x4A5C81 v2:0x495B82 v3:0x495B82 v4:0x475B85 v5:0x465B87 v6:0x455A88 v7:0x445A8A v8:0x435A8B v9:0x425A8D v10:0x41598F v11:0x405990 v12:0x3F5992 v13:0x3E5993 v14:0x3D5895 v15:0x3C5896 v16:0x3B5898 v17:0x3A589A]] atIndex:0];
  [facebookView addSubview:[self label:self.facebookLabel forView:facebookView withName:@"Facebook" loggedIn:NO]];
  [self.scrollView addSubview:facebookView];
}

When it's one subView it works fantastic, when I add the other one it's not right:

-(void)setUpLogInButtons {
  ...
  UIView *facebookView = [[UIView alloc] init];
  ...
  [self.scrollView addSubview:facebookView];

  UIView *twitterView = [[UIView alloc] init];
  twitterView.frame = CGRectMake(subviewWidth, 0, subviewWidth, subviewHeight);
  [twitterView.layer insertSublayer:[self gradientLayerForViewFrame:twitterView.frame withColors:[self colorArrayWithValues:0x2BA8D5 v2:0x28A8D6 v3:0x25A8D8 v4:0x22A8D9 v5:0x20A9DB v6:0x1DA9DC v7:0x1AA9DE v8:0x18AADF v9:0x15AAE1 v10:0x12AAE3 v11:0x10ABE4 v12:0x0DABE6 v13:0x0AABE7 v14:0x08ACE9 v15:0x05ACEA v16:0x02ACEC v17:0x00ADEE]] atIndex:0];
  [twitterView addSubview:[self label:self.twitterLabel forView:twitterView withName:@"Twitter" loggedIn:NO]];
  [self.scrollView addSubview:twitterView];
}

This one does not show up, the layers specifically are off. However, here is the kicker, the label shows in the right spot because the labels frame is based on it's parent view frame.

-(UILabel *)label:(UILabel *)viewLabel forView:(UIView *)accountView withName:(NSString *)name loggedIn:(BOOL)loggedinStatus {
  UILabel *label = viewLabel;
  label.frame = CGRectMake(0, accountView.frame.size.height - 50, accountView.frame.size.width, 50);
  label.text = [self setAccountLabelForName:name loggedIn:loggedinStatus];
  label.textColor = [UIColor whiteColor];
  label.textAlignment = NSTextAlignmentLeft;

  return label;
}

here is a picture that shows you that the label is based off the parents view frame, and it shows up correctly, but the gradient does not, and it's based off the same frame.

1个

and my gradient method:

-(CAGradientLayer *)gradientLayerForViewFrame:(CGRect)viewFrame withColors:(NSArray *)colors {
  CAGradientLayer *gradient = [CAGradientLayer layer];
  gradient.frame = viewFrame;
  gradient.colors = colors;

  return gradient;
}

For some reason when inserting my gradient layer to the views layer I had to change it:

//INSTEAD OF THIS:
[... insertSublayer:[self gradientLayerForViewFrame:facebookView.frame...];

//THIS WORKED FLAWLESSLY:
[... insertSublayer:[self gradientLayerForViewFrame:facebookView.bounds...];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM