gpt4 book ai didi

ios - 以编程方式将 UIButton 元素添加到 UIView 不会显示

转载 作者:行者123 更新时间:2023-11-29 10:24:08 25 4
gpt4 key购买 nike

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

我在自定义 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];
}

最佳答案

尝试用这个替换你的 - (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 中初始化按钮。它将起作用。

关于ios - 以编程方式将 UIButton 元素添加到 UIView 不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33290841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com