我正在使用最新的 SDK 开发一个 iOS 应用程序。
我有这个自定义 UIView:
#define ANNOTATION_WIDTH 250
#define ANNOTATION_HEIGHT 200
@implementation CustomView
- (id)initwithTitle:(NSString* )title subTitle:(NSString* )subTitle
{
CGRect annotationFrame =
CGRectMake(10, 10, ANNOTATION_WIDTH, ANNOTATION_HEIGHT);
if ([self initWithFrame:annotationFrame])
{
self.backgroundColor = [UIColor redColor];
self.opaque = YES;
/*
UILabel* nameLabel =
[[UILabel alloc] initWithFrame:CGRectMake(0, 0, ANNOTATION_WIDTH, 20.0)];
nameLabel.backgroundColor = [UIColor whiteColor];
nameLabel.textAlignment = NSTextAlignmentCenter;
//nameLabel.text = coordinate.name;
nameLabel.text = title;
[nameLabel sizeToFit];
[nameLabel setFrame: CGRectMake(0,
0,
nameLabel.bounds.size.width + 8.0,
nameLabel.bounds.size.height + 8.0)];
[self addSubview:nameLabel];
UILabel* placeLabel =
[[UILabel alloc] initWithFrame:CGRectMake(25, 0, ANNOTATION_WIDTH, 20.0)];
placeLabel.backgroundColor = [UIColor whiteColor];
placeLabel.textAlignment = NSTextAlignmentCenter;
//placeLabel.text = coordinate.place;
placeLabel.text = subTitle;
[placeLabel sizeToFit];
[placeLabel setFrame:CGRectMake(25,
0,
placeLabel.bounds.size.width + 8.0,
placeLabel.bounds.size.height + 8.0)];
[self addSubview:placeLabel];
*/
}
return self;
}
我将显示一个标题和一个子标题,但现在该代码是注释,因为我正在测试一些东西。
要将此自定义视图添加到 UIViewController 我这样做:
#import "ViewController.h"
#import "CustomView.h"
#import <QuartzCore/QuartzCore.h>
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CustomView* cView = [[CustomView alloc] initwithTitle:@"Titulo" subTitle:@"Subtitulo"];
cView.layer.cornerRadius = 10;
cView.layer.masksToBounds = YES;
[self.view addSubview:cView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
如果我没有显示标题和子标题,也没有看到我的 customView,但我显示了标题和子标题,我只会看到圆角的左上角。
我也试过这个,但它不起作用:
CustomView* cView = [[CustomView alloc] initwithTitle:@"Titulo" subTitle:@"Subtitulo"];
cView.layer.cornerRadius = 10;
cView.clipsToBounds = YES;
有没有办法在不添加任何子视图的情况下显示带有圆角的 UIView?
我不知道发生了什么,我也不知道为什么我只得到圆角左上角。