如何设置子视图到窗口的方向 [英] how to set orientation for a view which is subviewed to window
问题描述
我已经创建了一个UIView.我想将视图子视图到appdelegate窗口
I have created a UIView.I want to subview the view to the appdelegate window
UIView *newView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1048, 748)];
AppDelegate *appdelegate = [[UIApplication sharedApplication]delegate];
[appdelegate.window addSubview:newView];
这可以帮助我将视图细分为窗口视图,但是视图处于纵向模式.我需要将视图置于横向模式.如何将视图设置为横向模式?我希望红色视图完全覆盖白色视图.我该怎么做?
This helps me to subview a view to the window.But the view is in portrait mode.I need the view to be in landscape mode .How can I set the view to the landscape mode? and I want the Red color view to completely cover the white view.how can I do so?
RedColor是newView 存在白色viewController
RedColor is the newView white is present viewController
推荐答案
如果我们将任何对象添加到窗口,然后想要更改方向,则必须使用transform方法.
if we add any object to window and then we want to change the orientation then we must use transform methods.
#define DegreesToRadians(degrees) (degrees *M_PI /180)
添加到行上方
CGAffineTransform newTransform;
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
switch (orientation)
{
case UIInterfaceOrientationPortraitUpsideDown:
newTransform = CGAffineTransformMakeRotation(-DegreesToRadians(180));
txt.transform = newTransform;
txt.frame = CGRectMake(0, 0, 320, 480);
break;
case UIInterfaceOrientationLandscapeLeft:
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(-90));
txt.transform = newTransform;
txt.frame = CGRectMake(0, 0, 320, 480);
break;
case UIInterfaceOrientationLandscapeRight:
newTransform = CGAffineTransformMakeRotation(DegreesToRadians(90));
txt.transform = newTransform;
txt.frame = CGRectMake(0, 0, 320, 480);
break;
default:
newTransform = CGAffineTransformMakeRotation(-DegreesToRadians(0));
txt.transform = newTransform;
txt.frame = CGRectMake(0, 0, 320, 480);
break;
}
这里txt是对象名,请尝试这种方式.
here txt is object name,try in this way.
这篇关于如何设置子视图到窗口的方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!