ios 如何使用位置添加子视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3518804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:34:28  来源:igfitidea点击:

How to addSubview with a position?

iosobjective-cuiview

提问by DNB5brims

I have something like this:

我有这样的事情:

myViewController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
[mainCanvas addSubview: myViewController.view];
self.view = mainCanvas;

It will be added at the position (0, 0), but I want to add it at (0, 100) or somewhere else. How can I do so?

它将被添加到 (0, 0) 位置,但我想将它添加到 (0, 100) 或其他位置。我怎么能这样做?

回答by acqu13sce

Something like this:

像这样的东西:

myViewController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil];
myViewController.view.frame = CGRectMake(0, 100, myViewController.view.frame.size.width, myViewController.view.frame.size.height);  
[mainCanvas addSubview: myViewController.view];
self.view = mainCanvas;

回答by Dave DeLong

In addition to setting the frameproperty, you can also set the centerproperty of a view.

除了设置frame属性,还可以设置center视图的属性。

回答by Gary

Set the frame property on the sub view.

在子视图上设置 frame 属性。

回答by whyoz

This is the best way I've found to add a subView, like a loading screen or something that you want to show whether you are in a UIView, UIScrollView, or UITableView.

这是我发现添加一个子视图,如加载屏幕,或者你想显示你是否在一个东西的最佳方式UIViewUIScrollViewUITableView

myViewController = [[MyViewController alloc] initWithNibName:@"MyView" bundle:nil]; 
myViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:myViewController.view];
self.view.bounds = myViewController.view.bounds;

This will make the subView appear in full screen no matter where you are in the self.view by adding the subView to where you are currently located in your self.view instead of positioning it in the top left corner, only showing fully if you are at the very top of your view.

这将使子视图无论您在 self.view 中的哪个位置都以全屏显示在您的视野的最顶端。