以编程方式向UIView添加UIButton元素不会显示

时间:2015-10-22 21:12:08

标签: ios objective-c uiview uibutton

我正在编写一个自定义的UIView类,它有自己的子UIView。我希望这个UIView在superView中居中,里面有两个UIButtons,两个UILabel。

我在自定义UIView中使用我想要的参数覆盖我的init,在实现中的init方法中,我分配/初始化我的所有对象,在我的init方法结束时,我将subView添加到self。然后我将所有按钮和对象添加到subView。

问题是,即使在layoutSubviews中设置我的坐标和框架后,我的按钮和标签也不会出现在subView中。

这是我的方法代码:

@property (nonatomic, weak) UIView *alertSubView; // this will be the message view in middle of screen
@property (nonatomic, weak) UILabel *alertTitleLabel; // This will be the title within the alertSubView
@property (nonatomic, weak) UILabel *alertMessageLabel; // This will be the alert message under the alert title
@property (nonatomic, weak) UIButton *doNotAskButton; // This will be the do not ask button
@property (nonatomic, weak) UIButton *cancelButton; // This will be the cancel button, bottom left of alertView
@property (nonatomic, weak) UIButton *confirmButton; // This will be the confirm button, bottom right of alertView
@property (nonatomic) BOOL doNotAsk;

@end

@implementation MyAlertView

- (instancetype)initWithFrame:(CGRect)frame messageTitle:(NSString *)title messageSubject:(NSString *)subject shouldAskForDoNotAsk:(BOOL)doNotAsk delegate:(id<MyAlertViewDelegate>)delegate
{
if (self = [super initWithFrame:frame]) {
    self.delegate = delegate;

    UILabel *alertLabel = [[UILabel alloc] init];
    alertLabel.text = title;
    alertLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:15];
    self.alertTitleLabel = alertLabel;

    UILabel *messageLabel = [[UILabel alloc] init];
    messageLabel.text = subject;
    alertLabel.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:15];
    self.alertMessageLabel = messageLabel;

    self.backgroundColor = [UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:0.75];

    UIView *alertBoxView = [[UIView alloc] init];
    self.alertSubView = alertBoxView;

    [self.alertSubView addSubview:self.alertTitleLabel];
    [self.alertSubView addSubview:self.alertMessageLabel];
    if (doNotAsk) {
        UIButton *buttonDoNotAsk = [[UIButton alloc] init];
        buttonDoNotAsk.titleLabel.text = @"Do not ask me again";
        self.doNotAskButton = buttonDoNotAsk;
        [self.alertSubView addSubview:self.doNotAskButton];
    }
    UIButton *cancelButton = [[UIButton alloc] init];
    cancelButton.titleLabel.text = @"Cancel";
    cancelButton.titleLabel.textColor = [UIColor blueColor];
    cancelButton.backgroundColor = [UIColor whiteColor];
    cancelButton.opaque = YES;
    self.cancelButton = cancelButton;
    [self.alertSubView addSubview:self.cancelButton];

    UIButton *confirmButton = [[UIButton alloc] init];
    confirmButton.titleLabel.text = @"OK";
    self.confirmButton = confirmButton;
    [self.alertSubView addSubview:self.confirmButton];

    self.alertSubView.backgroundColor = [UIColor whiteColor];

    [self addSubview:self.alertSubView];
}
return self;
} 

- (void)layoutSubviews
{
// place the alertView in the center of self view
CGFloat alertHeight = kAlertHeightModifier * self.frame.size.height;
CGFloat alertWidth = kAlertWidthModifier * self.frame.size.width;
[self.alertSubView setFrame:CGRectMake(0, 0, alertWidth, alertHeight)];
[self.alertSubView addSubview:self.cancelButton];
[self setUpButtonsAndLabels];
[self.alertSubView setCenter:self.center];
}

- (void) setUpButtonsAndLabels {
CGFloat alertHeight = kAlertHeightModifier * self.frame.size.height;
CGFloat alertWidth = kAlertWidthModifier * self.frame.size.width;
CGFloat buttonWidth = 0.5 * alertWidth;
CGFloat buttonHeight = 0.2 * alertHeight;
[self.cancelButton setFrame:CGRectMake(15, 45, buttonWidth, buttonHeight)];
[self.confirmButton setFrame:CGRectMake(0, 0, buttonWidth, buttonHeight)];
[self.confirmButton setCenter:self.center];
}

2 个答案:

答案 0 :(得分:2)

尝试用此替换- (void)layoutSubviews

 - (void)layoutSubviews
    {
    // place the alertView in the center of self view
    CGFloat alertHeight = kAlertHeightModifier * self.frame.size.height;
    CGFloat alertWidth = kAlertWidthModifier * self.frame.size.width;
    [self.alertSubView setFrame:CGRectMake(0, 0, alertWidth, alertHeight)];
    [self.alertSubView addSubview:self.cancelButton];
    [self setUpButtonsAndLabels];
    [self.alertSubView setCenter:self.center];
    [super layoutSubviews];
    }

或者在viewdidload中初始化按钮。它会工作。

答案 1 :(得分:0)

尝试在viewDidLoad中执行此操作。应该有助于消除问题,因为视图将完全初始化并随时可以使用。