1
votes

I'm writing a custom UIView class that has its own sub UIView. I want this UIView to be centered inside the superView with two UIButtons inside of it, two UILabels.

I overwrite my init with the parameters I want in the custom UIView and in that init method in implementation, I am alloc/initializing all my objects and at the end of my init method, I add my subView to self. And I add all the buttons and objects to the subView.

The problem is, even after setting my coordinates and frames in layoutSubviews, my buttons and labels do not appear in the subView.

Here's my method code:

@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
1. You must call [super layoutSubview]; at the start of your layoutSubviews method. 2. After that, verify the view hierarchy is as expected. 3. After that, verify all frames are as expected.rmaddy

2 Answers

2
votes

Try replacing your - (void)layoutSubviews with this .

 - (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];
    }

Or else initialize the button in viewdidload .It will work .

0
votes

Try doing it in viewDidLoad. Should help to eliminate the problem, because the view will be fully initialized and ready to use.