【问题标题】:Objective C. UIView gradient background frame does not fit view boundsObjective C. UIView 渐变背景框架不适合视图边界
【发布时间】:2015-03-26 03:01:25
【问题描述】:

我在 Storyboard 中创建了两个 UIView。现在,我正在尝试为两者设置渐变背景:

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

现在,这就是我得到的:

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

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

...但无济于事。

谁能帮忙?

谢谢!

【问题讨论】:

    标签: objective-c uiview background gradient


    【解决方案1】:

    尝试代替

    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);
    

    【讨论】:

    • 我做到了。没有帮助。无论如何,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-14
    • 2023-03-19
    • 1970-01-01
    • 2022-10-14
    相关资源
    最近更新 更多