旋转时更改 UIScrollView 内的子视图框架

发布于 2024-09-09 02:20:29 字数 536 浏览 4 评论 0原文

我在 UIScrollView 中有几行子视图,我想在旋转到横向模式后重新排列它们。理想情况下,我希望每行有 5 个按钮,总行数由按钮高度和按钮总数(加上一些填充)决定。

我遇到的问题是如何迭代子视图,同时迭代该行上 5 个按钮的 x 位置,然后在达到 5 个按钮限制后更改 y 位置以开始新行。

现在我可以迭代子视图,但我不确定如何自动将框架设置为每行 5 个,然后更改 y 位置以开始新行。任何建议将不胜感激!

for (UIView *subview in someScrollView.subviews)
{
        if (subview.tag >= 100) {

            [subview setFrame: CGRectMake(x, y, buttonWidth, buttonHeight)];
            NSLog(@"%.1f %.1f %d %d on subview %d", x, y, buttonWidth, buttonHeight, subview.tag);

        } 

}

I have rows of subviews within a UIScrollView that I want to rearrange after rotating to landscape mode. Ideally, I want to have 5 buttons per row with the total number of rows determined by the button height and how many buttons there are total (plus some padding).

The problem I am having is how to iterate through subviews while also iterating through x-position for 5 buttons on the row and then changing y-position after I hit the 5 button limit to start a new row.

Right now I can iterate through subviews, but I'm not sure how to automate setting the frame for 5 per row and then changing the y-position to start a new row. Any suggestions are greatly appreciated!

for (UIView *subview in someScrollView.subviews)
{
        if (subview.tag >= 100) {

            [subview setFrame: CGRectMake(x, y, buttonWidth, buttonHeight)];
            NSLog(@"%.1f %.1f %d %d on subview %d", x, y, buttonWidth, buttonHeight, subview.tag);

        } 

}

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

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

发布评论

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

评论(1

莳間冲淡了誓言ζ 2024-09-16 02:20:29

我不明白...还是谢谢大家。

对于纵向模式:

    for (UIView *subview in outletScrollView.subviews)
    {
        if (x <= 320 && subview.tag != 0) {

            [subview setFrame: CGRectMake(x, y, buttonWidth, buttonHeight)];
            //NSLog(@"%.1f %.1f %d %d on subview %d", x, y, buttonWidth, buttonHeight, subview.tag);

            x = x + buttonWidth + gapX;

            if (x == 320) {
                x = gapX;
                y = y + buttonHeight + gapY;
            }

        }

    }

对于横向模式:

    for (UIView *subview in outletScrollView.subviews)
    {
        if (x <= 480 && subview.tag != 0) {

            [subview setFrame: CGRectMake(x, y, buttonWidth, buttonHeight)];
            //NSLog(@"%.1f %.1f %d %d on subview %d", x, y, buttonWidth, buttonHeight, subview.tag);

            x = x + buttonWidth + gapX;

            if (x == 480) {
                x = gapX;
                y = y + buttonHeight + gapY;
            }

        }

    }

I dun' figured it out... thanks all the same.

For portrait mode:

    for (UIView *subview in outletScrollView.subviews)
    {
        if (x <= 320 && subview.tag != 0) {

            [subview setFrame: CGRectMake(x, y, buttonWidth, buttonHeight)];
            //NSLog(@"%.1f %.1f %d %d on subview %d", x, y, buttonWidth, buttonHeight, subview.tag);

            x = x + buttonWidth + gapX;

            if (x == 320) {
                x = gapX;
                y = y + buttonHeight + gapY;
            }

        }

    }

For landscape mode:

    for (UIView *subview in outletScrollView.subviews)
    {
        if (x <= 480 && subview.tag != 0) {

            [subview setFrame: CGRectMake(x, y, buttonWidth, buttonHeight)];
            //NSLog(@"%.1f %.1f %d %d on subview %d", x, y, buttonWidth, buttonHeight, subview.tag);

            x = x + buttonWidth + gapX;

            if (x == 480) {
                x = gapX;
                y = y + buttonHeight + gapY;
            }

        }

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