如何从UIView的某些部分删除边框? [英] How to remove border from some part of UIView?
问题描述
我有一个 UIView
,其中包含其他子视图。我将边框应用于此 UIView
,并且边框应用于整个 UIView
。为此,请参阅第一个图像。
I am having a UIView
which contains other subviews. I am applying border to this UIView
and the border is applied to the whole UIView
. For that see the first image.
但不希望标题周围带有排行榜
的边框。我如何才能仅删除该部分的边框。参见下图,并且在标题排行榜周围没有边框。
But do not want the border around the title where it says "Leaderboard"
. How can i remove the border for that portion only. See the below image and in that see that there is no border around the header Leaderboard..
推荐答案
否, CALayer
边框不支持该行为。
NO,CALayer
borders don’t support that behavior.
但是如果您需要实现此方法,则可以尝试另一种方法,
尝试添加一个n点宽的不透明子视图,并将其所需的边框颜色作为其边框颜色
But you can try an alternate method if you need to implement this, try adding an n-point-wide opaque subview with your desired border color as its background color, on each side of your main view.
添加此代码:
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];
这只会在两侧添加边框。在顶部和底部也重复相同的想法。
This will add borders to both sides only.Repeat the same idea for top and bottom also.
NB : heightfromTop
是您不希望看到的顶部的高度要显示边框视图,您可以根据需要进行更改
NB : heightfromTop
is the height of the top section where you don't want the border view to be present, you can alter it as per your needs
这篇关于如何从UIView的某些部分删除边框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!