6

我有一个UIView包含其他子视图的。我正在对此应用边框,UIView并且边框将应用于整个UIView. 为此,请参见第一张图片。

在此处输入图像描述

但不希望在标题周围出现边框"Leaderboard"。我怎样才能只删除该部分的边框。请参见下图,其中看到标题排行榜周围没有边框。

在此处输入图像描述

4

2 回答 2

2

不,CALayer边框不支持这种行为。

但是,如果您需要实现此功能,您可以尝试另一种方法,尝试在主视图的每一侧添加一个 n 点宽的不透明子视图,并将所需的边框颜色作为其背景颜色。

添加此代码:

CGSize mainViewSize = theView.bounds.size;
CGFloat borderWidth = 2;
UIColor *borderColor = [UIColor redColor];
CGFloat heightfromTop = 25;
UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, heightfromTop borderWidth, mainViewSize.height-heightfromTop)];
UIView *rightView = [[UIView alloc] initWithFrame:CGRectMake(mainViewSize.width - borderWidth, heightfromTop, borderWidth, mainViewSize.height-heightfromTop)];
leftView.opaque = YES;
rightView.opaque = YES;
leftView.backgroundColor = borderColor;
rightView.backgroundColor = borderColor;

[mainView addSubview:leftView];
[mainView addSubview:rightView];

这只会在两侧添加边框。对顶部和底部也重复相同的想法。

注意heightfromTop是您不希望出现边框视图的顶部的高度,您可以根据需要更改它

于 2013-03-13T06:29:41.130 回答
1

您可以子类化UIView并实现drawRect类似:

- (void)drawRect:(CGRect)rect
{       
      float borderSize = 3.0f;

     //draw the bottom border
     CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
     CGContextFillRect(context, CGRectMake(0.0f, self.frame.size.height - borderSize, self.frame.size.width, borderSize));

     //draw the right border
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextFillRect(context, CGRectMake(0.0f,0.0f, borderSize,self.frame.size.height));

     //draw the left border 
    CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextFillRect(context, CGRectMake(self.frame.size.width - borderSize,0.0f, borderSize,self.frame.size.height));
}

现在,您需要使用子类UIView来创建所需的视图。

于 2013-03-13T06:41:01.213 回答