目标C. UIView渐变背景框架不适合视图边界

时间:2015-03-26 03:01:25

标签: objective-c uiview background gradient

我在故事板中创建了两个UIViews。现在,我正在尝试为两者设置渐变背景:

-(void)drawView
{
    // Set the Views Background Color
    [self.depMetarView setBackgroundColor:[UIColor clearColor]];
    [self.destMetarView setBackgroundColor:[UIColor clearColor]];

    // Draw a custom gradient
    CAGradientLayer *depMetarGradient = [IGABackgroundLayer gradientBlue];
    CAGradientLayer *destMetarGradient = [IGABackgroundLayer gradientBlue];
    depMetarGradient.frame = self.depMetarView.bounds;
    destMetarGradient.frame = self.destMetarView.bounds;
    [self.depMetarView.layer insertSublayer:depMetarGradient atIndex:0];
    [self.destMetarView.layer insertSublayer:destMetarGradient atIndex:0];

    // Round button corners
    [self.depMetarView.layer setMasksToBounds:YES];
    [self.depMetarView.layer setCornerRadius:5.0];
    [self.destMetarView.layer setMasksToBounds:YES];
    [self.destMetarView.layer setCornerRadius:5.0];

    // Apply a 1 pixel, black border
    [self.depMetarView.layer setBorderWidth:1.0f];
    [self.depMetarView.layer setBorderColor:[[UIColor blueColor] CGColor]];
    [self.destMetarView.layer setBorderWidth:1.0f];
    [self.destMetarView.layer setBorderColor:[[UIColor blueColor] CGColor]];
}

现在,这就是我得到的:

enter image description here

我知道视图的界限有问题。试过这个:

CGRect depViewBounds = CGRectMake(self.depMetarView.frame.origin.x,self.depMetarView.frame.origin.y,self.depMetarView.frame.size.width, self.depMetarView.frame.size.height);
[self.depMetarView setBounds:depViewBounds];

......但无济于事。

有人可以帮忙吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试代替

CGRect depViewBounds = CGRectMake(self.depMetarView.frame.origin.x,self.depMetarView.frame.origin.y,self.depMetarView.frame.size.width, self.depMetarView.frame.size.height);

这个:

CGRect depViewBounds = CGRectMake(0,0,self.depMetarView.frame.size.width, self.depMetarView.frame.size.height);