好的,所以我有一个非常基本的应用程序,其中包含一个按钮。我将控制器设置为仅允许横向。我的问题是,在初始化之后,然后我单击我的按钮(它只有一个日志语句),不同于我在init结束时的日志语句。
我在模拟器上以横向模式启动应用程序(尽管设备上的结果相同)。就像我分配它一样,它只是切换回来。我在我的buttonClicked方法中尝试了这个语句self.frame = CGRectMake(0, 0, 1024, 768);
,但这只是扭曲并转移它。
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
//BUTTONS
attributesButton = [UIButton buttonWithType:UIButtonTypeCustom];
attributesButton.frame = CGRectMake(self.frame.size.width - buttonPadding - 35, self.frame.size.height/2 -22 -150, 35, 35);
[attributesButton setBackgroundImage:[UIImage imageNamed:@"loadIcon.png"] forState:UIControlStateNormal];
[attributesButton addTarget:self action:@selector(attributesButtonClicked)
forControlEvents:UIControlEventTouchUpInside];
[attributesButton setTitle:@"Attr" forState:UIControlStateNormal];
[self addSubview:attributesButton];
NSLog(@"Width: %f",self.frame.size.width);
NSLog(@"Height: %f",self.frame.size.height);
}
return self;
}
-(void)attributesButtonClicked
{
NSLog(@"Width: %f",self.frame.size.width);
NSLog(@"Height: %f",self.frame.size.height);
}
这是我的初学者。对不起它看起来很可怕我不知道为什么。我的视图控制器:
- (void)loadView
{
NSLog(@"myViewController: loadView");
myView = [[myView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.view = myView;
}
现在这是获取我的部分,即日志声明。
2010-08-27 15:16:55.242 tester[8703:40b] myViewController: loadView
2010-08-27 15:16:55.262 tester[8703:40b] Width: 1024.000000
2010-08-27 15:16:55.262 tester[8703:40b] Height: 768.000000
CLICK MY BUTTON HERE
2010-08-27 15:17:05.689 tester[8703:40b] Width: 748.000000
2010-08-27 15:17:05.689 tester[8703:40b] Height: 1024.000000
答案 0 :(得分:1)
根据我的经验,看起来即使你处于横向模式,如果你正在使用自动调整遮罩,你需要在使用initWithFrame初始化视图时使用“纵向”框架:
尝试:
myView = [[myView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
答案 1 :(得分:0)
如果您的视图绑定到其中一个标准视图控制器(UINavigationController,UITabBarController,...),则无论您在 initWithFrame 中指定了什么,控制器都会在运行时更新帧大小。
这实际上是件好事,因为你不必担心方向,工具栏占用空间等等。
答案 2 :(得分:0)
这可能是因为当您将视图添加为子视图时,父视图会自动恢复视图。
如果您不希望这种情况发生(尽管您通常用于全屏视图),可以将autoresizingMask
设置为UIViewAutoresizingNone
:
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
...
self.autoresizingMask = UIViewAutoresizingNone;
}
return self;
}
编辑:好的,对不起,我只记得UIViewAutoresizingNone实际上是autoresizingMask的默认值,所以在初始化期间将它设置为该值实际上不会改变任何东西。但关键是当您将视图添加为子视图时,超级视图会更改框架。