5

是否可以制作圆角(topLeft 和 topRight)来自动调整uiview?这是我的代码:

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; 

}

该视图在 IB 中定义为:

国际文凭组织

我得到的 - 右上角是直的,因为视图的大小是动态的。

结果

4

2 回答 2

2

您需要将UIView' 的contentMode属性设置为UIViewContentModeRedrew. 内容模式控制视图的内容在其边界更改时如何更改(例如自动调整大小时)。默认情况下,它只是拉伸视图的内容,这就是你的角被拉伸的原因。

于 2012-05-28T15:32:07.667 回答
1

解决方案:

感谢 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);

}

结果:

结果

于 2012-05-28T16:35:30.450 回答