以编程方式添加的子视图框架不会在滚动视图中显示

时间:2015-09-04 20:52:14

标签: ios objective-c scrollview

我以编程方式创建了一个scrollView,并在其中创建了所有视图。但是,当我添加超过1个视图时,其他视图图层不会显示在各自的帧中。

例如:

-(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];
}

然后我将subViews设置为scrollView:

-(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];
}

当它是一个子视图时它很棒,当我添加另一个时它不对:

-(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];
}

这个显示,图层特别关闭。但是,这里是踢球者,标签显示在正确的位置,因为标签框架基于它的父视图框架。

-(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;
}

这是一张图片,显示标签基于父视图框架,并且它正确显示,但渐变不显示,并且它基于相同的框架。

1

和我的渐变方法:

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

  return gradient;
}

1 个答案:

答案 0 :(得分:0)

出于某种原因,当我将渐变图层插入到视图图层时,我必须更改它:

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

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