【问题标题】:iPad uiview frame changes right after initWithFrameiPad uiview 框架在 initWithFrame 之后立即更改
【发布时间】:2010-08-27 19:29:20
【问题描述】:

好的,我有一个非常基本的应用程序,其中包含一个按钮的视图。我将控制器设置为仅允许横向。我的问题是,在它初始化之后,然后我点击我的按钮(它只有一个日志语句),与我在初始化结束时的日志语句不同。

我在模拟器上以横向模式启动应用程序(但在设备上的结果相同)。就像一旦我分配它,它就会切换回来。我在我的 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

【问题讨论】:

    标签: iphone ipad uiview size frame


    【解决方案1】:

    根据我的经验,即使您处于横向模式,如果您使用自动调整大小的蒙版,则在使用 initWithFrame 初始化视图时需要使用“纵向”框架:

    试试:

    myView = [[myView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
    

    【讨论】:

      【解决方案2】:

      如果您的视图绑定到标准视图控制器之一(UINavigationController、UITabBarController...),无论您在 initWithFrame 中指定什么,控制器都会在运行时更新帧大小。

      这实际上是一件好事,因为您不必担心方向、工具栏占用空间等问题。

      【讨论】:

      • 我的观点与标准控制器无关。只是:@interface myViewController : UIViewController
      • 它不必直接绑定到标准视图控制器。您的“myViewController”可能在 UINavigationController 或 UITabBarController 中使用。这会给你同样的行为
      【解决方案3】:

      这可能是因为当您将视图添加为子视图时,父视图会自动调整其大小。 如果您不希望这种情况发生(尽管您通常会在全屏视图中这样做),您可以将 autoresizingMask 设置为 UIViewAutoresizingNone

      - (id)initWithFrame:(CGRect)frame 
      {
          if ((self = [super initWithFrame:frame])) 
          {
              ...
              self.autoresizingMask = UIViewAutoresizingNone;
          }
          return self;
      }
      

      编辑:好的,抱歉,我只记得 UIViewAutoresizingNone 实际上是 autoresizingMask 的默认值,因此在初始化期间将其设置为该值实际上不会改变任何内容。但关键是当您将视图添加为子视图时,超级视图会更改框架。

      【讨论】:

      • 那没有帮助。谢谢你的想法。如果我在横向,为什么它会调整纵向大小?
      • 我没有添加子视图,我只是将 self.view 设置为 myView。当我尝试添加子视图时,我什么也没得到(因为我的视图为零)
      • 是的,但是其他一些对象(可能是应用程序委托?)将采用该视图并将其作为子视图添加到其他视图(可能是主应用程序窗口),否则您不会根本看不到,对吧?这就是框架发生变化的时候。
      猜你喜欢
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多