How to remove border from some part of UIView?

I have a UIView that contains other subheadings. I apply a border to this UIView , and the border applies to the whole UIView . To do this, see the first image.

enter image description here

But you don’t need the border around the title to say "Leaderboard" . How can I remove the border for this part only. Look at the image below and see that there is no frame around the Leaderboard header.

enter image description here

+4
source share
2 answers

NO, CALayer borders do not support this behavior.

But you can try an alternative method, if you need to implement this, try adding an opaque n-point-wide sub-layer with the desired border color as the background color on each side of your main view.

Add this code:

 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 for both sides only. Repeat the same idea for the top and bottom.

NB : heightfromTop is the height of the top where you don’t want the border view present, you can change it to suit your needs.

+2
source

You can subclass UIView and implement drawRect as:

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

Now you need to use the UIView subclass to create the required view.

+1
source

Source: https://habr.com/ru/post/1468896/


All Articles