iOS-为圆角矩形创建UIView子类 [英] iOS - Create UIView subclass for rounded rectangle
问题描述
我正在尝试创建&对圆角矩形使用非常简单的UIView子类.我创建了一个新类,如下所示:
I'm trying to create & use a very simple UIView subclass for a rectangle with rounded corners. I've created a new class as follows :
RoundedRect.h
RoundedRect.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface RoundedRect : UIView
@end
RoundedRect.m
RoundedRect.m
#import "RoundedRect.h"
@implementation RoundedRect
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[[self layer] setCornerRadius:10.0f];
[[self layer] setMasksToBounds:YES];
}
return self;
}
@end
我将iOS 5.1与情节提要板一起使用,并且已将IB检查器窗口中的自定义类属性设置为"RoundedRect",但是当我运行该应用程序时,矩形仍然具有正方形角.我错过了明显的事情吗?
I'm using iOS 5.1 with storyboards and have set the custom class property in the IB inspector window to 'RoundedRect', but when I run the app the rectangle still has square corners. Have I missed something obvious?
谢谢乔纳森
推荐答案
从XIB文件实例化视图时,不会调用 initWithFrame
方法.而是调用 initWithCoder:
初始化程序,因此您需要在此方法中执行相同的初始化.
The initWithFrame
method is not called when the view is instantiated from a XIB file. Instead, the initWithCoder:
initializer is called, so you need to perform the same initialization in this method.
这篇关于iOS-为圆角矩形创建UIView子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!