0

最新の 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 を表示する方法はありますか?

何が起こっているのかわからず、なぜ左上隅だけが丸くなるのかわかりません。

4

2 に答える 2

0

これを試しましたか

[self.layer setCornerRadius:10];
[self.layer setMasksToBounds:YES];
于 2012-12-27T11:13:51.390 に答える
0

これを試して:

cView.layer.cornerRadius = 10.0f;
cView.layer.masksToBounds = YES;
cView.layer.opaque = NO;
[self.view addSubview:cView];
于 2012-12-27T11:36:00.810 に答える