2012-05-28 2 views
5

자동 크기 조절에 둥근 모서리 (위쪽 왼쪽 및 위쪽 오른쪽)를 사용할 수 있습니까?자동 크기 조정을위한 둥근 모서리 UIView

IB

: 뷰는 다음과 같이 IB에 정의되어 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; 

} 

SFDetailViewController.h

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

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

@end 

: 다음은 내 코드입니다

내가보기에 뷰의 크기가 동적이기 때문에 topRight 모서리는 직선입니다.

result

답변

2

당신은 UIViewContentModeRedrew 같은에 UIViewcontentMode 속성을 설정해야합니다. 콘텐츠 모드는 경계가 변경되면 (예 : 자동 크기 조정과 같이)보기의 콘텐츠가 변경되는 방식을 제어합니다. 기본적으로보기의 내용 만 늘어나므로 모서리가 늘어납니다.

1

는 해결책 : 내가 쓴 그 (void)drawRect:(CGRect)rect :

[header setNeedsDisplay]; 

어떤은, 그런데, 호출

덕분에 jbrennan하는, 내가있는 viewDidLoad에 내가 전화

header.contentMode = UIViewContentModeRedraw; 

그런

설정 다음 :

- (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