self.view.frame 和 self.superview.frame 有什么区别以及如何使用它们?
我想知道这两行代码有什么区别?
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view.superview.frame = CGRectMake(0, 0, 800, 900);
当我的方向改变时,我想改变视图框架,因为它会改变标签的位置,并且我希望它们位于屏幕中间,任何人都可以指导我吗?
我正在使用以下委托方法进行定向,但它不起作用
self.view.frame
,但可以使用以下行正常工作,
self.view.superview.frame
请参阅以下代码
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"LEFT");
self.view.frame = CGRectMake(100, 0, 480, 320);
NSLog(@"Show self.view.frame: %@", NSStringFromCGRect(self.view.frame));
// self.view.superview.frame = CGRectMake(-50, -70, 800, 900);
[button setFrame:CGRectMake(340, 320, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"RIGHT");
self.view.frame = CGRectMake(0, 0, 320, 480);
NSLog(@"Show self.view.frame: %@", NSStringFromCGRect(self.view.frame));
//self.view.superview.frame = CGRectMake(10, 90, 800, 900); //It is working if I will uncomment it
[button setFrame:CGRectMake(250, 300, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.frame = CGRectMake(0, 0, 320, 480);
//self.view.superview.frame = CGRectMake(0, 0, 800, 900);//It is working if I will uncomment it
[button setFrame:CGRectMake(250, 400, 100, 30)];
}
return YES;
}
I would like to know that what is difference between these both lines of code?
self.view.frame = CGRectMake(0, 0, 320, 480);
self.view.superview.frame = CGRectMake(0, 0, 800, 900);
and I want to change the view frame when my orientation will change, because it will change the position of labels, and I want them in middle of screen, can anyone guide me ?
I am using following delegate method, for orientation, but it is not working with
self.view.frame
but it is working ok with following line
self.view.superview.frame
See the following code
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
//return (interfaceOrientation == UIInterfaceOrientationPortrait);
if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
NSLog(@"LEFT");
self.view.frame = CGRectMake(100, 0, 480, 320);
NSLog(@"Show self.view.frame: %@", NSStringFromCGRect(self.view.frame));
// self.view.superview.frame = CGRectMake(-50, -70, 800, 900);
[button setFrame:CGRectMake(340, 320, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
NSLog(@"RIGHT");
self.view.frame = CGRectMake(0, 0, 320, 480);
NSLog(@"Show self.view.frame: %@", NSStringFromCGRect(self.view.frame));
//self.view.superview.frame = CGRectMake(10, 90, 800, 900); //It is working if I will uncomment it
[button setFrame:CGRectMake(250, 300, 100, 30)];
}
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.frame = CGRectMake(0, 0, 320, 480);
//self.view.superview.frame = CGRectMake(0, 0, 800, 900);//It is working if I will uncomment it
[button setFrame:CGRectMake(250, 400, 100, 30)];
}
return YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
self.view 是 self 的视图(如果我们谈论的是 viewController)。
self.view.superview 是持有 self.view 的视图。
因此,简而言之,如果向窗口添加视图,则该视图的超级视图将是窗口。
如果自动调整大小蒙版设置不正确,设置框架将会失败。
self.view
is the view of self (if we are talking about viewControllers).self.view.superview
is the view that is holdingself.view
.So, in short, if you add a view to the window the superview of that view will be the window.
Setting the frame will fail if the autoresize mask isn't set correctly.
作为通用语句,第一行尝试设置编写此代码的当前 viewController 视图的框架。
第二行尝试设置该视图的父视图的框架当前 viewController 的视图。
这到底意味着什么,以及您应该使用哪一个恐怕取决于您在应用程序中设置的视图层次结构。
As a generic statement, the first line is trying set the frame of the current viewController's view that this code is written in.
The second line is trying to set the frame of the parent view of the view of the current viewController's view.
What this exactly means, and which one you should be using I'm afraid depends on the view hierarchy you have set up in your application.