I have created the UIButton with specific position corner radius means left button have top left and bottom left corner radius and right button have top right and bottom right corner radius i have take the button in Xib and give border but i cut from some side i have attach the screenshot below.
Look at right side corner border cut. here is my code
[btn_Newest setBackgroundColor:RGB(28.0, 91.0, 161.0, 1.0)];
[btn_Newest.layer setBorderWidth:1.5];
[btn_Newest.layer setBorderColor:RGB(28.0, 91.0, 161.0, 1.0).CGColor];
[btn_Newest setClipsToBounds:YES];
btn_Newest = (UIButton *)[self setroundCornersOnView:btn_Newest onTopLeft:YES topRight:NO bottomLeft:YES bottomRight:NO radius:7.0];
[btn_Popular setBackgroundColor:viewTopBar.backgroundColor];
[btn_Popular.layer setBorderWidth:1.5];
[btn_Popular.layer setBorderColor:RGB(28.0, 91.0, 161.0, 1.0).CGColor];
[btn_Popular setClipsToBounds:YES];
btn_Popular = (UIButton *)[self setroundCornersOnView:btn_Popular onTopLeft:NO topRight:YES bottomLeft:NO bottomRight:YES radius:7.0];
setroundCornersOnView this function create specific side corner radius.Can any one help me in this situation.
Kindly try the following code. i think it will satisfy your requirements.
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btnPush.bounds byRoundingCorners:(UIRectCornerTopRight | UIRectCornerBottomRight) cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
btnPush.layer.mask = maskLayer;
CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.view.bounds;
borderLayer.path = maskPath.CGPath;
borderLayer.lineWidth = 1.5f;
borderLayer.strokeColor = [UIColor blackColor].CGColor;
borderLayer.fillColor = [UIColor clearColor].CGColor;
[btnPush.layer addSublayer:borderLayer];
Output :
Instead of setting setroundCornersOnView .You can set cornerRadius to the button.
try below code.
[btn_Newest.layer setCornerRadius:5];
Related
I want to add radius to bottom left and bottom right corners of an UIView and then drop shadow only at bottom of the same UIView.
I have gone through solutions in which all corners are provided with radius and then shadow. That is working fine. But when I use UIBeizerPath to add radius to bottom corners the shadow property doesn't seem to work.
I am using Objective-C and XCode 8.1.
How can I do it?
Using below code bottom corners get their radius but shadow properties doesn't work.
UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 40)];
[testView setBackgroundColor:[UIColor yellowColor]];
// shadow
testView.layer.shadowColor = [UIColor colorWithRed:156.0f/255.0f green:153.0f/255.0f blue:153.0f/255.0f alpha:1.0f].CGColor;
testView.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
testView.layer.shadowRadius = 4.0f;
testView.layer.shadowOpacity = 0.5f;
UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:CGPointMake(0.0, 0.0)];
[path addLineToPoint:CGPointMake(0.0, CGRectGetHeight(testView.frame))];
[path addLineToPoint:CGPointMake(CGRectGetWidth(testView.frame), CGRectGetHeight(testView.frame))];
[path addLineToPoint:CGPointMake(CGRectGetWidth(testView.frame), 0.0)];
testView.layer.shadowPath = path.CGPath;
//bottom corners radius
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:testView.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(2.0, 2.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
testView.layer.mask = maskLayer;
The mask is masking the shadow. You need to have two view one inside the other. Apply the mask to the inner view and the shadow to the outer view.
I have two uibuttons that a right next to eachother. I would like to make them look joined by giving the left button rounded top left and bottom left corners and the right button top right and bottom right corners.
I have seen several solutions to this but all I have seen break the uieffectview I have behind the button.
You can do it like this:
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.viewOutlet.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.view.bounds;
maskLayer.path = maskPath.CGPath;
self.viewOutlet.layer.mask = maskLayer;
Update:
If You need border just create another CAShapeLayer and add it to view's layer as sublayer. Like this (place this code below upper code):
CAShapeLayer *borderLayer = [[CAShapeLayer alloc] init];
borderLayer.frame = self.view.bounds;
borderLayer.path = maskPath.CGPath;
borderLayer.lineWidth = 4.0f;
borderLayer.strokeColor = [UIColor blackColor].CGColor;
borderLayer.fillColor = [UIColor clearColor].CGColor;
[self.viewOutlet.layer addSublayer:borderLayer];
I have a UICollectionView (iPad) that has jarry scrolling.Each cell has a white rectangle UIView (with all rounded corners) that has another UIImageView in front of it with only top two corners rounded. See this link below for how this cell looks - i.stack.imgur.com/Ptlig.png
Here's the code for the UICollectionViewCell
UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, (self.contentView.frame.size.width - 10), (self.contentView.frame.size.width - 10)*IMAGE_RATIO + 43.5)];
bgView.backgroundColor = [UIColor clearColor];
bgView.layer.cornerRadius = 10;
bgView.layer.borderWidth = 0.5f;
bgView.layer.borderColor = [UIColor colorWithRed: 179/255.0 green:181/255.0 blue:184/255.0 alpha:1.0].CGColor;
bgView.layer.backgroundColor = [UIColor whiteColor].CGColor;
bgView.layer.shouldRasterize = YES;
bgView.layer.rasterizationScale = [UIScreen mainScreen].scale;
[self.contentView addSubview:bgView];
self.parablePost = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, (self.contentView.frame.size.width - 10), (self.contentView.frame.size.width-10)*IMAGE_RATIO)];
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.parablePost.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.parablePost.bounds;
maskLayer.path = maskPath.CGPath;
self.parablePost.layer.mask = maskLayer;
[self.contentView addSubview:self.parablePost];
When this line "self.parablePost.layer.mask = maskLayer" is commented, scrolling is smooth. So the CAShapeLayer is causing performance issues. I tried rasterize and hand drawing instead of using UIBezierPath but it does not improve the experience.
Is there a faster way to round two corners of UIImageView?
EDIT:
I finally added static images to simulate rounded corners on the top two corners of the UIImageView that gave me the same look without the scroll jitter. Surprised that CAShapeLayer is a performance drain.
masks are expensive... (they might even be rendered in software). try to think of another way to get the look you want. for example, pre-render things to an image...
I'm creating uiview button
'Button = [UIButton buttonWithType:UIButtonTypeCustom];
with color at selected state and normal state.
The point is i want to change mask. To make my button with one side radius corners.
to do this I'm using metod:
- (void)cornerMaskRadiusAtButton:(UIButton*)button leftSide:(BOOL)side
{
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = button.bounds;
UIBezierPath *roundedPath;
if(side){
roundedPath =[UIBezierPath bezierPathWithRoundedRect:maskLayer.bounds
byRoundingCorners:UIRectCornerTopLeft |
UIRectCornerBottomLeft
cornerRadii:CGSizeMake(18.f, 18.f)];
}
else{
roundedPath = [UIBezierPath bezierPathWithRoundedRect:maskLayer.bounds
byRoundingCorners:UIRectCornerTopRight |
UIRectCornerBottomRight
cornerRadii:CGSizeMake(18.f, 18.f)];
}
maskLayer.path = [roundedPath CGPath];
button.layer.mask = maskLayer;
}
but the result is like this:
after I call this metods:
[self cornerMaskRadiusAtButton:Button leftSide:NO];
Button.layer.borderColor = [UIColor whiteColor].CGColor;
Button.layer.borderWidth = 1.;
but the the border is wrong.
How to fix the border line ?
U will not see your layer border beacouse u are cuting off it with ur layer mask.
It's why u see only fragment of it and other not. They are cut.
The best way to do it is to creat a graphic for the buttons and replace bacgroundimage
for example:
icon = [UIImage imageNamed:#"name"];
[Button setBackgroundImage:icon forState:UIControlStateNormal];
U can set for other state to like: UIControlStateSelected
some other image.
I have a custom button that I want to make top-left border of it look like normal round rectangle.
I found code that makes all corners round:
_myButton.layer.cornerRadius = 8;
_myButton.layer.borderWidth = 0.5;
_myButton.layer.borderColor = [UIColor grayColor].CGColor;
_myButton.clipsToBounds = YES;
How can I fix the code to make it round just in the top-left side?
Edit:
_myButton.layer.borderWidth = 2;
_myButton.layer.borderColor = [UIColor blackColor].CGColor;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:_myButton.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = _myButton.bounds;
maskLayer.path = maskPath.CGPath;
_myButton.layer.mask = maskLayer;
[maskLayer release];
This code does not work. The whole button disappears.
You almost got it, but, after building your CAShapeLayer, use it to add itself as a sublayer of your button's layer, not as an alpha mask to hide some parts of your button (in that case the corners).
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(10,300,300,40);
[button setTitle:#"Hey" forState:(UIControlStateNormal)];
[button setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:button.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(7.0, 7.0)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = button.bounds;
shapeLayer.path = shapePath.CGPath;
shapeLayer.fillColor = [UIColor clearColor].CGColor;
shapeLayer.strokeColor = [UIColor blackColor].CGColor;
shapeLayer.lineWidth = 2;
[button.layer addSublayer:shapeLayer];
[self.view addSubview:button];
CAShapeLayer * positiveCorner = [CAShapeLayer layer];
positiveCorner.path = [UIBezierPath bezierPathWithRoundedRect: self.button.bounds
byRoundingCorners: UIRectCornerTopRight | UIRectCornerBottomRight
cornerRadii: (CGSize){5, 5}].CGPath;
self.button.layer.mask = positiveCorner;