我正在实现一个 Custom View
在 ViewController
内我用不同的参数调用了两次我的应用程序,但这有一个奇怪的行为。
正如您在图片中看到的 views
是一组UILabel
s 和 2 UIImageView
s,两个 Custom View
s 是相等的,实际上创建这两个 View 的代码相同。
我用颜色知道每个 Label
在哪里并查看 background
.灰色和橙色是 2 backgrounds
.第一个 View 可以代替它,但第二个属性在真实 View 之外(灰色的)。任何人都知道像我一样实例化 View 是否有任何问题?有没有更好的办法?
我已使用此代码实例化此 views
在 ViewController
:
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:self.sherpaViewDescubridor];
[self.view addSubview:self.sherpaViewConquistador];
}
#pragma mark - Custom Getters
-(WPSherpaView *)sherpaViewDescubridor
{
if(!sherpaViewDescubridor){
CGRect sherpaFrame = CGRectMake(10, 0, 320, 125);
sherpaViewDescubridor = [[WPSherpaView alloc] initWithFrame:sherpaFrame];
return sherpaViewDescubridor;
}
return sherpaViewDescubridor;
}
-(WPSherpaView *)sherpaViewConquistador
{
if(!sherpaViewConquistador){
CGRect sherpaFrame = self.sherpaViewDescubridor.frame;
sherpaFrame.origin.y = CGRectGetMaxY(sherpaFrame) + 5;
sherpaViewConquistador = [[WPSherpaView alloc] initWithFrame:sherpaFrame];
sherpaViewConquistador.backgroundColor = [UIColor grayColor];
return sherpaViewConquistador;
}
return sherpaViewConquistador;
}
这是自定义 View 代码:
@implementation WPSherpaView
@synthesize aliasLbl, nameLbl ,pointsLbl,positionLbl,profileImg,flagImg, viewNameLbl, warCryLbl;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CGRect firstLabelFrame = {
.origin.x = kMarginLeft,
.origin.y = self.frame.origin.y + 10.0 ,
.size.width = self.frame.size.width - (2*kMarginLeft),
.size.height = kNameViewHeight,
};
viewNameLbl = [[WPCustomLabel alloc] initWithFrame:firstLabelFrame];
[viewNameLbl setFont:[UIFont systemFontOfSize:KMediumFontSize]];
[viewNameLbl setBackgroundColor:[UIColor redColor]];
viewNameLbl.textAlignment = NSTextAlignmentLeft;
[self addSubview:viewNameLbl];
CGRect centreViewFrame = firstLabelFrame;
centreViewFrame.origin.y = CGRectGetMaxY(firstLabelFrame) + 10.0;
UIView *centerView = [[UIView alloc] initWithFrame:centreViewFrame];
centerView.backgroundColor = [UIColor greenColor];
[self addSubview:centerView];
// LEFT IMAGES
CGRect imageProfileFrame = {
.origin.x = 0,
.origin.y = 0 ,
.size.width = kImageSize,
.size.height = kImageSize,
};
profileImg = [[UIImageView alloc] initWithFrame:imageProfileFrame];
profileImg.backgroundColor = [UIColor blueColor];
[centerView addSubview:profileImg];
imageProfileFrame.origin.x = CGRectGetMaxX(imageProfileFrame)+ kMarginLeft;
flagImg = [[UIImageView alloc] initWithFrame:imageProfileFrame];
flagImg.backgroundColor = [UIColor orangeColor];
[centerView addSubview:flagImg];
// RIGHT LABELS
CGRect labelFrame = imageProfileFrame;
labelFrame.origin.x = CGRectGetMaxX(imageProfileFrame) + (2 * kMarginLeft);
labelFrame.size = kSizelabel;
aliasLbl = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[aliasLbl setFont:[UIFont systemFontOfSize:KMediumFontSize]];
[aliasLbl setBackgroundColor:[UIColor blackColor]];
aliasLbl.textAlignment = NSTextAlignmentLeft;
[centerView addSubview:aliasLbl];
labelFrame.origin.y += kSizelabel.height;
labelFrame.size.height = 15.0;
nameLbl = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[nameLbl setFont:[UIFont systemFontOfSize:KMinFontSize]];
[nameLbl setBackgroundColor:[UIColor grayColor]];
nameLbl.textAlignment = NSTextAlignmentLeft;
[centerView addSubview:nameLbl];
labelFrame.origin.y += kSizelabel.height;
positionLbl = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[positionLbl setFont:[UIFont systemFontOfSize:13]];
[positionLbl setBackgroundColor:[UIColor orangeColor]];
positionLbl.textAlignment = NSTextAlignmentLeft;
[centerView addSubview:positionLbl];
labelFrame.origin.y += kSizelabel.height;
warCryLbl = [[WPCustomLabel alloc] initWithFrame:labelFrame];
[warCryLbl setFont:[UIFont systemFontOfSize:13]];
[warCryLbl setBackgroundColor:[UIColor blackColor]];
warCryLbl.textAlignment = NSTextAlignmentLeft;
[centerView addSubview:warCryLbl];
centreViewFrame.size.height = CGRectGetMaxY(labelFrame) + 5 ;
centerView.frame = centreViewFrame;
// BORDER VIEW
CGRect bordeFrame = self.frame;
bordeFrame.size.height = 0.5;
bordeFrame.origin.y = self.frame.size.height - 0.5;
bordeFrame.origin.x = kMarginLeft;
UIView *bottomBorder = [[UIView alloc] initWithFrame:bordeFrame];
bottomBorder.backgroundColor = [UIColor grayColor];
[self addSubview:bottomBorder];
}
return self;
}
谢谢
最佳答案
试试这个
CGRect firstLabelFrame = {
.origin.x = kMarginLeft,
.origin.y = self.bounds.origin.y + 10.0 , // <----
.size.width = self.bounds.size.width - (2*kMarginLeft), // <-----
.size.height = kNameViewHeight,
};
关于ios - 自定义 UIViews 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20199235/