วันอาทิตย์ที่ 1 กันยายน พ.ศ. 2556

how to set cornerRadius for only top-left and top-right corner of a UIView

Here's a method - I'm sure it could be shortened.... :D
-(UIView *)roundCornersOnView:(UIView *)view onTopLeft:(BOOL)tl topRight:(BOOL)tr bottomLeft:(BOOL)bl bottomRight:(BOOL)br radius:(float)radius {

UIRectCorner corner; //holds the corner


//Determine which corner(s) should be changed
if (tl) {
    corner = UIRectCornerTopLeft;
}
if (tr) {
    corner = UIRectCornerTopRight;
}
if (bl) {
    corner = UIRectCornerBottomLeft;
}
if (br) {
    corner = UIRectCornerBottomRight;
}
if (tl && tr) { //top
    corner = UIRectCornerTopRight | UIRectCornerTopLeft;
}
if (bl && br) { //bottom
    corner = UIRectCornerBottomLeft | UIRectCornerBottomRight;
}
if (tl && bl) { //left
    corner = UIRectCornerTopLeft | UIRectCornerBottomLeft;
}
if (tr && br) { //right
    corner = UIRectCornerTopRight | UIRectCornerBottomRight;
}

if (tl & tr & bl & br) {
    corner = UIRectCornerAllCorners; 
}

UIView *roundedView = view;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:roundedView.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = roundedView.bounds;
maskLayer.path = maskPath.CGPath;
roundedView.layer.mask = maskLayer;

return roundedView;
}
    UIButton *openInMaps = [UIButton new];
    [openInMaps setFrame:CGRectMake(15, 135, 114, 70)];
    openInMaps = (UIButton *)[self roundCornersOnView:openInMaps onTopLeft:NO topRight:NO bottomLeft:YES bottomRight:NO radius:5.0];