我有使用代碼(無界面構建器或故事板)創建的視圖控制器。我試圖以編程方式添加幾個視圖(按鈕和圖像視圖),我使用砌體AutoLayout。使用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
,似乎沒有其他信息的唯一視圖。
此外,在過去的2個約束添加到recordButton
崩潰的應用程序和錯誤是「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>>
」 但是當我腳按鈕,沒有錯誤,但它畢竟不是在視圖中可見。任何想法爲什麼意見沒有出現,代碼有問題?
嗨尼蒂什,用的UIButton或任何UIControl子類的問題是,它有一個類的方法來初始化。 [UIButton buttonWithType:]在UIButton的情況下。使用createAndAddSubView創建視圖將創建UIView,而使用[UIButton buttonWithType:]創建將創建一個UIControl,它是一個按鈕。 self.recordButton = [self.view createAndAddSubView:UIButton.class]; self.recordButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];這兩個將創建兩個單獨的視圖,所以第二個語句將覆蓋第一個 – 2016-08-10 17:32:30