Objective-C –在视图内定位视图

发布于 2024-12-05 18:55:31 字数 133 浏览 2 评论 0原文

有没有办法向视图添加一些子视图,然后能够定位该视图,以便所有子视图都将“跟随”。可以说,充当子视图的容器,因此我只需要重新定位视图,而不必重新定位该视图中的所有子视图。

我正在寻找一种解决方案来以编程方式执行此操作,而不是在 IB 中。

Is there a way to add some subviews to a view and then be able to position that view so all the subviews will "follow". Acting as a container so to speak for the subviews so I only need to reposition the view and not having to reposition all the subviews in that view.

I'm looking for a solution to do this programtically, not in IB.

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

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

发布评论

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

评论(1

蓝咒 2024-12-12 18:55:31

如果您使用 UIView 作为父视图,则所有子视图都会跟随。

UIView *parentView = [[UIView alloc] initWithFrame:self.view.bounds]
[self.view addSubview:parentView]; // Assuming self is a ViewController
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(10, 10, 120, 44);
[parentView addSubview:btn1];

// Sets a new position with the same view size
parentView.frame = CGRectMake(50, 100, self.view.bounds.size.width, self.view.bounds.size.height);

现在当你设置parentView的新框架时。 btn1 将具有相对于parentView 的位置。

我希望我正确理解你的问题。

编辑:
添加评论

If you use a UIView as a parent view all subviews will follow.

UIView *parentView = [[UIView alloc] initWithFrame:self.view.bounds]
[self.view addSubview:parentView]; // Assuming self is a ViewController
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn1.frame = CGRectMake(10, 10, 120, 44);
[parentView addSubview:btn1];

// Sets a new position with the same view size
parentView.frame = CGRectMake(50, 100, self.view.bounds.size.width, self.view.bounds.size.height);

Now when you set the new frame of the parentView. The btn1 will have it's position relative to the parentView.

I Hope I understood your question correctly.

EDIT:
Added comments

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