我有一个使用代码创建的视图控制器(没有界面构建器或故事板)。我正在尝试以编程方式添加一些视图(按钮和图像视图),我正在使用Masonry进行AutoLayout。
我有以下代码
- (instancetype)init {
self = [super init];
if (self) {
self.view.backgroundColor = UIColor.whiteColor;
self.progress = 0.f;
self.shouldRecord = YES;
[self createMainUI];
[self addActions];
}
return self;
}
- (void)createMainUI {
self.backButton = [self.view createAndAddSubView:UIButton.class];
[self.backButton setImage:[TeamieUIGlobals defaultPicForPurpose:@"close" withSize:25.f] forState:UIControlStateNormal];
[self.backButton addTarget:self action:@selector(dismissViewController) forControlEvents:UIControlEventTouchUpInside];
self.micNormalImageView = [self.view createAndAddSubView:UIImageView.class];
self.dynamicProgress = [self.view createAndAddSubView:TMEProgressImageView.class];
self.dynamicProgress.image = IMAGE_MIC_WAVE;
self.dynamicProgress.progress = 0;
self.dynamicProgress.hasGrayscaleBackground = NO;
self.dynamicProgress.verticalProgress = YES;
self.recordButton = [self.view createAndAddSubView:UIButton.class];
self.recordButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.recordButton.backgroundColor = UIColor.redColor;
self.recordButton.layer.cornerRadius = 50.f;
self.recordButton.titleLabel.font = [TeamieGlobals appFontFor:@"regularFontWithSize13"];
[self.recordButton setTitle:@"Record" forState:UIControlStateNormal];
[self.recordButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
[self.backButton mas_makeConstraints:^(MASConstraintMaker *make){
make.width.height.equalTo(@25);
make.left.top.equalTo(@10);
}];
[self.micNormalImageView mas_makeConstraints:^(MASConstraintMaker *make){
make.width.equalTo(@204);
make.height.equalTo(@295);
make.center.equalTo(self.view);
}];
[self.dynamicProgress mas_makeConstraints:^(MASConstraintMaker *make){
make.width.equalTo(@204);
make.height.equalTo(@295);
make.center.equalTo(self.view);
}];
[self.recordButton mas_makeConstraints:^(MASConstraintMaker *make){
make.width.height.equalTo(@100);
make.centerX.equalTo(self.view);
make.bottom.equalTo(self.view.mas_bottom).with.offset(15.f);
}];
}
这是createAndAddSubView
做的事情
- (id)createAndAddSubView:(Class)modelClass {
id temporaryView = [modelClass new];
[self insertSubview:(UIView *)temporaryView atIndex:0];
return temporaryView;
}
模拟器中视图上显示的唯一视图是backButton
,并且不会显示其他视图。
此外,添加到recordButton
的最后2个约束会导致应用崩溃,错误为“couldn't find a common superview for <UIButton: 0x7ff2eb15f110; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x7ff2eb118c60>> and <UIView: 0x7ff2e8f4bf20; frame = (0 0; 320 568); autoresize = W+H; layer = <CALayer: 0x7ff2e8fdf220>>
”
但是当我按下按钮时,没有错误但仍然在视图上看不到。知道为什么视图没有出现,代码有问题吗?
答案 0 :(得分:1)
- (id)createAndAddSubView:(Class)modelClass {
id temporaryView = [modelClass new];
[self insertSubview:(UIView *)temporaryView atIndex:self.subviews.count];
return temporaryView;
}
问题在于atIndex:0
它应该是self.subviews.count
答案 1 :(得分:0)
我认为问题可能是您在父视图中插入子视图的索引。替换[[self subviews] count]
代替索引&#39; 0&#39; as-
- (id)createAndAddSubView:(Class)modelClass {
id temporaryView = [modelClass new];
[self insertSubview:(UIView *)temporaryView atIndex:[[self subviews] count]];
return temporaryView;
}
答案 2 :(得分:0)
上面提到的代码有几个问题: 1.如果错过了这个,请道歉,但是我没有看到设置每个子视图的帧的代码。 这是不正确的。
self.recordButton = [self.view createAndAddSubView:UIButton.class];
self.recordButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
3。您将在先前添加的子视图之上添加每个子视图。
请检查所有这些方面,我认为您的代码应该有效。