添加子视图/约束:试图了解为什么我的子视图没有显示

汤姆

我想创建一个弹出窗口,让用户知道他们是否尝试输入重复信息。所以最初我有一个 UILabel 会弹出然后消失。

        UILabel *toastView = [[UILabel alloc] init];

        toastView.alpha = 0;
        toastView.layer.cornerRadius = 4.0f;
        toastView.layer.borderColor = [UIColor colorWithRed:200.0/255.0 green:199.0/255.0 blue:204.0/255.0 alpha:1].CGColor;
        CGFloat scale = [[UIScreen mainScreen] scale];
        if (scale == 2.0)  {
            // retina screen;
            toastView.layer.borderWidth = 0.5;
        } else {
            // non-retina screen
            toastView.layer.borderWidth = 1.0;
        }
        toastView.backgroundColor = [UIColor colorWithRed:63.0/255.0 green:63.0/255.0 blue:63.0/255.0 alpha:1];
        toastView.textAlignment = NSTextAlignmentCenter;
        [toastView setAdjustsFontSizeToFitWidth:YES];
        toastView.text = @" E-Mail has already been added  ";
        toastView.font = [UIFont fontWithName:@"HelveticaNeue" size:toastView.font.pointSize];
        toastView.textColor = [UIColor whiteColor];
        [toastView setTranslatesAutoresizingMaskIntoConstraints:NO];
        [toastView.layer setMasksToBounds:YES];
        [self.superview.superview.superview addSubview:toastView];
        [[UIApplication sharedApplication].keyWindow bringSubviewToFront:toastView];

        [toastView.bottomAnchor constraintEqualToAnchor:self.superview.superview.superview.centerYAnchor].active = YES;
        [toastView.centerXAnchor constraintEqualToAnchor:self.superview.superview.superview.centerXAnchor].active = YES;
        [toastView.heightAnchor constraintEqualToConstant:round(self.superview.superview.superview.frame.size.height/12)].active = YES;
        [toastView.widthAnchor constraintEqualToConstant:round(self.superview.superview.superview.frame.size.width/1.5)].active = YES;

        [UIView animateWithDuration:0.3 animations:^{
            toastView.alpha = 0.95;
        } completion:^(BOOL finished) {
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.3 animations:^{
                    toastView.alpha = 0;
                } completion:nil];
            });
        }];

这工作得很好。然后我想添加一个图像,所以我决定将这个弹出窗口的创建移动到一个方法,该方法接受一个视图和一条消息,并生成一个带有 UILabel 和 UIImageView 的弹出视图。这是代码:

+(void)toastAlert:(UIView*)view message:(NSString*)message {
    UIView *toastView = [[UIView alloc] init];
    UILabel *toastLabel = [[UILabel alloc] init];
    UIImage *infoImage = [UIImage imageNamed:@"info_white"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:infoImage];

    toastView.alpha = 0;
    toastView.layer.cornerRadius = 4.0f;
    toastView.layer.borderColor = [UIColor colorWithRed:200.0/255.0 green:199.0/255.0 blue:204.0/255.0 alpha:1].CGColor;
    CGFloat scale = [[UIScreen mainScreen] scale];
    if (scale == 2.0)  {
        // retina screen;
        toastView.layer.borderWidth = 0.5;
    } else {
        // non-retina screen
        toastView.layer.borderWidth = 1.0;
    }

    [toastLabel setTextAlignment:NSTextAlignmentCenter];
    [toastLabel setAdjustsFontSizeToFitWidth:YES];
    [toastLabel setText:[NSString stringWithFormat:@"%@", message]];
    [toastLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:toastLabel.font.pointSize]];
    [toastLabel setTextColor: [UIColor whiteColor]];

    toastView.backgroundColor = [UIColor colorWithRed:63.0/255.0 green:63.0/255.0 blue:63.0/255.0 alpha:1];
    [toastView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [toastView.layer setMasksToBounds:YES];
    [view addSubview:toastView];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:toastView];
    [toastView addSubview:toastLabel];
    [toastView addSubview:imageView];

    [toastView.bottomAnchor constraintEqualToAnchor:view.centerYAnchor].active = YES;
    [toastView.centerXAnchor constraintEqualToAnchor:view.centerXAnchor].active = YES;
    [toastView.heightAnchor constraintEqualToConstant:round(view.frame.size.height/12)].active = YES;
    [toastView.widthAnchor constraintEqualToConstant:round(view.frame.size.width/1.5)].active = YES;

    [imageView.centerYAnchor constraintEqualToAnchor:toastView.centerYAnchor].active = YES;
    [imageView.leadingAnchor constraintEqualToAnchor:toastView.leadingAnchor constant:padding].active = YES;
    [imageView.heightAnchor constraintEqualToConstant:toastView.frame.size.height/2].active = YES;
    [imageView.widthAnchor constraintEqualToAnchor:toastView.heightAnchor].active = YES;


    [toastLabel.centerYAnchor constraintEqualToAnchor:toastView.centerYAnchor].active = YES;
    [toastLabel.centerXAnchor constraintEqualToAnchor:toastView.centerXAnchor constant:imageView.frame.size.width + padding*2].active = YES;
    [toastLabel.leadingAnchor constraintEqualToAnchor:imageView.trailingAnchor constant:padding].active = YES;
    [toastLabel.heightAnchor constraintEqualToConstant:toastView.frame.size.height/2].active = YES;


    [UIView animateWithDuration:0.3 animations:^{
        toastView.alpha = 0.95;
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.3 animations:^{
                toastView.alpha = 0;
            } completion:nil];
        });
    }];

}

然而,无论我做什么,我都只能让 UIView 出现,而​​没有文本或 UILabel 出现。当我添加 UIImageView 时,它会显示出来,但会弄乱整个 toastView 的大小调整。

我仍然是一名初级开发人员,我确定我错过了一些基本的东西......谁能向我解释我做错了什么?(不仅仅是寻找一个直接的答案,因为解释更有价值)

阿吉布森007

所以我认为你的自动调整蒙版与你的约束相冲突。那并从 CGRectZero 开始。我将尝试解决每个项目,以便您知道出了什么问题。这是工作代码。

 -(void)alteredToastAlert:(UIView*)targetView message:(NSString*)message {

    CGFloat padding = 10.0;
    // i like to start with real frames buggy things happen with CGRectZero
    UIView *toastView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
    //turn all resizing masks off
    [toastView setTranslatesAutoresizingMaskIntoConstraints:NO];

    UILabel *toastLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 200)];
    [toastLabel setTranslatesAutoresizingMaskIntoConstraints:NO];

    UIImage *infoImage = [UIImage imageNamed:@"info_white"];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:infoImage];
    [imageView setTranslatesAutoresizingMaskIntoConstraints:NO];
    //has a size but intrinsic

    toastView.alpha = 0;
    toastView.layer.cornerRadius = 4.0f;
    toastView.layer.borderColor = [UIColor colorWithRed:200.0/255.0 green:199.0/255.0 blue:204.0/255.0 alpha:1].CGColor;
    CGFloat scale = [[UIScreen mainScreen] scale];

    if (scale == 2.0)  {
        // retina screen;
        toastView.layer.borderWidth = 0.5;
    } else {
        // non-retina screen
        toastView.layer.borderWidth = 1.0;
    }

    [toastLabel setTextAlignment:NSTextAlignmentCenter];
    [toastLabel setText:[NSString stringWithFormat:@"%@", message]];
    [toastLabel setFont:[UIFont fontWithName:@"HelveticaNeue" size:22]];
    [toastLabel setTextColor: [UIColor whiteColor]];

    [toastLabel setMinimumScaleFactor:0.01];

    toastView.backgroundColor = [UIColor colorWithRed:63.0/255.0 green:63.0/255.0 blue:63.0/255.0 alpha:1];
    [toastView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [toastView.layer setMasksToBounds:YES];
    [targetView addSubview:toastView];
    [[UIApplication sharedApplication].keyWindow bringSubviewToFront:toastView];
    //adding here but everything has a frame so it is not size 0.  Then apply autolayout
    [toastView addSubview:toastLabel];
    [toastView addSubview:imageView];

    [toastView.bottomAnchor constraintEqualToAnchor:targetView.centerYAnchor].active = YES;
    [toastView.centerXAnchor constraintEqualToAnchor:targetView.centerXAnchor].active = YES;
    [toastView.heightAnchor constraintEqualToConstant:round(targetView.frame.size.height/12)].active = YES;
    [toastView.widthAnchor constraintEqualToConstant:round(targetView.frame.size.width/1.5)].active = YES;

    [imageView.centerYAnchor constraintEqualToAnchor:toastView.centerYAnchor].active = YES;
    [imageView.leadingAnchor constraintEqualToAnchor:toastView.leadingAnchor constant:padding].active = YES;
    [imageView.heightAnchor constraintEqualToConstant:toastView.frame.size.height/2].active = YES;
    [imageView.widthAnchor constraintEqualToAnchor:toastView.heightAnchor].active = YES;
    imageView.backgroundColor = [UIColor redColor];



    [toastLabel.centerYAnchor constraintEqualToAnchor:toastView.centerYAnchor].active = YES;
    [toastLabel.leadingAnchor constraintEqualToAnchor:imageView.trailingAnchor constant:padding].active = YES;
   // have to have a trailing to keep it inside the view
    [toastLabel.trailingAnchor constraintEqualToAnchor:toastView.trailingAnchor constant:padding].active = YES;
    [toastLabel.heightAnchor constraintEqualToConstant:toastView.frame.size.height/2].active = YES;

    toastView.alpha = 1;
    [UIView animateWithDuration:0.3 animations:^{
        toastView.alpha = 0.95;
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [UIView animateWithDuration:0.3 animations:^{
                toastView.alpha = 0;
            } completion:^(BOOL finished){
                //may need to remove from superview
            }];
        });
    }];

}

1)你会注意到我设置了框架开始。我这样做是因为我注意到从 CGRectZero 开始做了一些疯狂的事情。我们最终会让视图由自动布局管理,但首先我设置框架。imageview 将建立它的内在框架。

2) 接下来我告诉系统不要从自动调整大小的掩码中创建约束。我为 toastView 这样做,就像标签和图像视图一样。

3) 我告诉标签文本的最小比例因子,并从任意字体大小的值 22 开始。您可能会将其提高到 50,但它仍会按比例缩小。我想因为 CGRectZero 你以前的字体大小是 0 但我不确定

4)我让您的约束与标签上的尾随约束版本一起工作,但我认为就是这样。你可以看看。

5) 当所有这一切都完成时,不要忘记从它的超级视图或您正在传递的视图中删除 toastView,因为即使 alpha 为 0,它仍然存在。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我的子视图没有添加到我的滚动视图?

当我以编程方式添加一个没有约束的子视图时会发生什么?

试图向视图添加子视图,但子视图不显示

为什么没有绘制超出我的自定义视图边界的子视图?

即使添加了子视图,也没有为约束准备视图层次结构

为什么我的 UIView 的 UILabel 子视图不显示?

为什么我的WKWebView没有显示在swiftUI视图中?

Django:为什么我的视图没有显示为 HTML

迅速为什么这个滚动视图不显示子视图?

为什么添加子图层会重叠子视图?

添加具有约束的子视图不起作用

为什么我在PHPMyAdmin的“关系视图”中没有“更新时”约束?

CAGradientLayer 添加的视图不显示子视图

快速向UIScrollView子视图添加约束

iOS:为什么我们需要在添加视图时添加子视图控制器作为子视图工作?

没有子视图的快照视图,或访问子视图的方法

为什么我的 UIView 动画在添加子视图后不起作用?

为什么我的UIButton不显示在子视图中?iPad应用程式

如何在StackView的子视图内的视图中添加约束

为什么我的文本没有显示在我导入的视图中?

没有在初始渲染中显示骨架.js子视图

为什么将项目添加到 ObservableCollection 时我的视图没有更新

为什么 Android Studio Design UI 没有正确显示我的视图?

为什么我的 Angular Material 表单元素没有显示在视图中?

当显示键盘时,为什么我的视图没有调整大小?

为什么我的领域回收视图没有显示任何数据?

为什么我的Firestore数据没有显示为表格视图?

为什么我的新子视图叠加在我的拆分视图中?

为什么我的Razor“ RenderSection”没有被孙子视图继承?