如何将 UIButtons 和 UILabels 添加到可滚动视图?

发布于 2024-11-30 09:06:56 字数 1939 浏览 0 评论 0原文

我几乎看过有关类似问题的所有问题,并尝试了给出的答案,但它们对我不起作用。事情是:我有一个主视图,并且有一个按钮,单击时,它会在其上添加一个新按钮和一个标签,依此类推,直到您“填充”该区域,并且我希望它可以调整大小和可滚动,这样您就可以看到所有按钮,并且可以放大以仅查看某些按钮。我不知道我是否清楚地解释了自己......

所以,我尝试将所有内容添加到 UIView,但它不起作用,我尝试将所有内容添加到 UIScrollView,但它再次不起作用。我不知道还能做什么。我几乎整个星期都在处理这个问题,但没有成功。我不得不说我对此经验不是很丰富,我大约 4 个月前才开始编写 iOS 代码,所以请耐心等待。我非常感谢您可以分享的任何帮助或指导。

这就是我正在做的事情:

- (IBAction) doneAdding:(id) sender{
        boxes = boxes+1;
        UIButton *newBox = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *boxImage = [UIImage imageNamed:@"bluecube.jpg"];

    if (boxes <= 4) {
    switch (boxes) {
        case 1:
            newBox.frame = CGRectMake(519, 356, 162, 163);
            break;
        case 2:
            newBox.frame = CGRectMake(681, 519, 162, 163);
            break;
        case 3:
            newBox.frame = CGRectMake(357, 519, 162, 163);
            break;
        case 4:
            newBox.frame = CGRectMake(519, 844, 162, 163);
            break;
            default:
            break;
    }

    [newBox setBackgroundImage:boxImage forState:UIControlStateNormal];
    [newBox addTarget:self action:@selector(goToProject:) forControlEvents:UIControlEventTouchUpInside];
    [homeView addSubview:newBox];


            //I get the text of the label from a textfield 
    UILabel *nameLabel= [ [UILabel alloc ] initWithFrame:CGRectMake(480.0,500.0, 150.0, 43.0) ];
    [[NSUserDefaults standardUserDefaults] setObject: newName.text forKey: @"SomeRandomText"];
    nameLabel.textAlignment =  UITextAlignmentCenter;
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.font = [UIFont fontWithName:@"Helvetica" size:(12.0)];
    nameLabel.text = [NSString stringWithFormat:@"%@", newName.text];
    [homeView addSubview:nameLabel];
    [newName release];


     }else {
        NSLog(@"No more boxes allowed");
     }
}

I've seen almost every question about similar issues, and tried the given answers, but they are not working for me. The thing is: I have a home view, and I have a button, on click, it adds a new button and a label over it, and so on until you "fill" the area, and I want it to be resizable and scrollable, so you can see all the buttons and you can zoom-in to see just some buttons. I don't know if I'm explaining myself clearly...

So, I tried adding everything to a UIView, and it didn't work, I tried adding everything to a UIScrollView, and again it didn't work. I don't know what else to do. I've been dealing with this almost all week without any success. I have to say I am not very experienced on this, I just started to code for iOS about 4 months ago, so please be patient. I would really appreciate any help or guidance you can share.

Here's what I am doing:

- (IBAction) doneAdding:(id) sender{
        boxes = boxes+1;
        UIButton *newBox = [UIButton buttonWithType:UIButtonTypeCustom];
        UIImage *boxImage = [UIImage imageNamed:@"bluecube.jpg"];

    if (boxes <= 4) {
    switch (boxes) {
        case 1:
            newBox.frame = CGRectMake(519, 356, 162, 163);
            break;
        case 2:
            newBox.frame = CGRectMake(681, 519, 162, 163);
            break;
        case 3:
            newBox.frame = CGRectMake(357, 519, 162, 163);
            break;
        case 4:
            newBox.frame = CGRectMake(519, 844, 162, 163);
            break;
            default:
            break;
    }

    [newBox setBackgroundImage:boxImage forState:UIControlStateNormal];
    [newBox addTarget:self action:@selector(goToProject:) forControlEvents:UIControlEventTouchUpInside];
    [homeView addSubview:newBox];


            //I get the text of the label from a textfield 
    UILabel *nameLabel= [ [UILabel alloc ] initWithFrame:CGRectMake(480.0,500.0, 150.0, 43.0) ];
    [[NSUserDefaults standardUserDefaults] setObject: newName.text forKey: @"SomeRandomText"];
    nameLabel.textAlignment =  UITextAlignmentCenter;
    nameLabel.textColor = [UIColor blackColor];
    nameLabel.backgroundColor = [UIColor clearColor];
    nameLabel.font = [UIFont fontWithName:@"Helvetica" size:(12.0)];
    nameLabel.text = [NSString stringWithFormat:@"%@", newName.text];
    [homeView addSubview:nameLabel];
    [newName release];


     }else {
        NSLog(@"No more boxes allowed");
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗酒趁年少 2024-12-07 09:06:56

你说的“它不起作用”是什么意思?您是否尝试将 UIScrollView 的 contentSize 属性设置为足够大以包含所有子视图的框架?

What do you mean with "it doesn't work"? Did you try setting the UIScrollView's contentSize property to a size large enough to contain all the subview's frames?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文