2015-04-03 83 views
0

我有使用代碼(無界面構建器或故事板)創建的視圖控制器。我試圖以編程方式添加幾個視圖(按鈕和圖像視圖),我使用砌體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>>」 但是當我腳按鈕,沒有錯誤,但它畢竟不是在視圖中可見。任何想法爲什麼意見沒有出現,代碼有問題?

回答

0

我認爲這個問題可能是您在父視圖中插入子視圖的索引。替代[[self subviews] count]代替索引「0」的原樣

- (id)createAndAddSubView:(Class)modelClass { 
    id temporaryView = [modelClass new]; 
    [self insertSubview:(UIView *)temporaryView atIndex:[[self subviews] count]]; 
    return temporaryView; 
} 
0

有關於代碼的若干關注上面給出: 1.道歉,如果錯過了這一點,但是我沒有看到一個代碼來設置每個子視圖的幀。 2.這是不正確的。

self.recordButton = [self.view createAndAddSubView:UIButton.class]; 
self.recordButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

3.您將在以前添加的子視圖之上添加每個子視圖。

請檢查所有這些方面,我認爲你的代碼應該工作。

1
- (id)createAndAddSubView:(Class)modelClass { 
    id temporaryView = [modelClass new]; 
    [self insertSubview:(UIView *)temporaryView atIndex:self.subviews.count]; 
    return temporaryView; 
} 

問題是與atIndex:0它應該是self.subviews.count

+0

嗨尼蒂什,用的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

相關問題