Jon*_*ham 11 subclass objective-c storyboard uiview ios
我正在尝试为一个带圆角的矩形创建和使用一个非常简单的UIView子类.我创建了一个新类,如下所示:
RoundedRect.h
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface RoundedRect : UIView
@end
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
我正在使用iOS 5.1和故事板,并在IB检查器窗口中将自定义类属性设置为'RoundedRect',但是当我运行应用程序时,矩形仍然有方角.我错过了一些明显的事吗?
谢谢乔纳森
And*_*sky 23
在iOS 5及更高版本中,绝对不需要子类 - 您可以从Interface Builder完成所有操作.
Pau*_*l.s 18
其他人已经回答了这个问题,但我会像这样重构它,以便在nib和代码中使用
#import "RoundedRect.h"
@implementation RoundedRect
- (id)initWithFrame:(CGRect)frame;
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder;
{
self = [super initWithCoder:aDecoder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit;
{
CALayer *layer = self.layer;
layer.cornerRadius = 10.0f;
layer.masksToBounds = YES;
}
@end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11182 次 |
| 最近记录: |