Round corners for auto installation UIView

Is it possible to make rounded corners ( topLeft and topRight ) to automatically configure uiview? Here is my code:

SFDetailViewController.h

@interface SFDetailViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate, PopoverViewListDelegate>
{
  ...

  UIView *header;
}
@property (nonatomic, retain) IBOutlet UIView *header;

@end

SFDetailViewController.m

#import "SFDetailViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface SFDetailViewController ()
@end


@implementation SFDetailViewController
@syntesyze header;

-(void) viewDidLoad
{
    ....
    [self setCornerRadiusToHeader:header];
 }



-(void) setCornerRadiusToHeader:(UIView *)headerView
{    
    CGRect bounds = headerView.layer.bounds;
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds 
                                               byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
                                                     cornerRadii:CGSizeMake(8.0, 8.0)];

    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = bounds;
    maskLayer.path = maskPath.CGPath;

    [headerView.layer addSublayer:maskLayer];
    headerView.layer.mask = maskLayer; 

}

The view is defined in IB as follows:

IB

What I get is the upper right corner of the line, because the size of the view is dynamic.

result

+5
source share
2 answers

You need to set the property UIView contentModeto something like UIViewContentModeRedrew. Content mode controls how content in a view changes when its boundaries change (for example, when it is authorized). By default, it just stretches the contents of the view, so your corners are stretched.

+2
source

DECISION:

jbrennan,

header.contentMode = UIViewContentModeRedraw;

viewDidLoad :

[header setNeedsDisplay];

, , : (void)drawRect:(CGRect)rect, :

- (void)drawRect:(CGRect)rect
{ 
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextClearRect(context, rect); 

    UIColor *color = [UIColor lightGrayColor];
    CGContextSetFillColorWithColor(context, color.CGColor);

    CGRect rrect = CGRectMake(CGRectGetMinX(rect)-2, CGRectGetMinY(rect), CGRectGetWidth(rect)+4, CGRectGetHeight(rect) + 1);
    CGFloat radius = 10.0f;

    CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
    CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);

    CGContextMoveToPoint(context, minx, midy);
    CGContextAddArcToPoint(context, minx, miny, midx, miny, radius);
    CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius);
    CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, 0);
    CGContextAddArcToPoint(context, minx, maxy, minx, midy, 0);
    CGContextClosePath(context);
    CGContextDrawPath(context, kCGPathFill);

}

:

result

+1

All Articles