【问题标题】:setFrame won't do anythingsetFrame 不会做任何事情
【发布时间】:2013-04-19 16:31:56
【问题描述】:

我的应用程序无法在 -init 和 -viewWillLayoutSubviews 方法之外设置框架。当一个人点击编辑按钮时应该发生什么是一个隐藏编辑器视图的动画。尽管如此,当我测试它时,什么都没有发生。问题不是来自动画方法,因为 -setFrame 方法因为它 - 不包含在块中 - 也不起作用。

代码如下:

-(id)init {

    if (self = [super init]) {

        editButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editButtonTapped)];
        doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonTapped)];


        editor = [[UIView alloc] init];
        [editor setBackgroundColor:[UIColor yellowColor]];
        editor.clipsToBounds = YES;
        editorIsOpen = YES;        

        portraitRegularModeEditorRect = CGRectMake(15, 59, 738, 100);
        portraitClosedEditorEditorRect = CGRectMake(15, 59, 738, 0);


    }
    return self;

}


- (void)viewDidLoad {

    [super viewDidLoad];

    [[self view] addSubview:editor];
    [[self view] setBackgroundColor:[UIColor blueColor]];

}


-(void)viewDidAppear:(BOOL)animated {

    [self setForRegularMode];

}




-(void)viewWillLayoutSubviews {

    UIInterfaceOrientation io = [[UIApplication sharedApplication] statusBarOrientation];
    if (io == UIInterfaceOrientationPortrait || io == UIInterfaceOrientationPortraitUpsideDown) {

        //portrait
        [editor setFrame:portraitRegularModeEditorRect];


     } else {

        //landscape

     }


}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


-(void)editButtonTapped {

    [self setForScenarioLinesEditingMode];

}

-(void)doneButtonTapped {

    [self setForRegularMode];

}


-(void)setForRegularMode {

    editingMode = CPRegularMode;    

    if (!editorIsOpen) {

        [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationCurveEaseOut animations:^(void){

            [editor setFrame:portraitRegularModeEditorRect];

        } completion:^(BOOL finished) {

            editorIsOpen = YES;

        }];

    }


    [[self navigationItem] setRightBarButtonItems:[[NSArray alloc] initWithObjects:editButton,nil]];

}



-(void)setForScenarioLinesEditingMode {

    editingMode = CPScenarioLinesEditingMode;

    if (editorIsOpen) {

        [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationCurveEaseOut animations:^(void){

            [editor setFrame:portraitClosedEditorEditorRect];

        } completion:^(BOOL finished) {

            editorIsOpen = NO;

        }];

    }

    [[self navigationItem] setRightBarButtonItems:[[NSArray alloc] initWithObjects:doneButton,nil]];


}

如果有人可以提供帮助,请提前感谢 ;)

【问题讨论】:

  • 你的setFrame:方法真的被调用了吗?
  • 我希望我知道。我能说的是之前和之后的一切都被称为。我该如何检查?
  • 调试器,例如。或者只是NSLog frame 和`编辑器。
  • 好吧,显然调用了setFrame: 方法,因为当我NSLog(@"%f",editor.frame.size.height); 在调用该方法之后,控制台告诉我正确的高度——0 或100——但视图仍然保持不变。
  • 经过一些随机测试,我意识到-viewWillLayoutSubviews:方法是在setFrame:方法被调用之后被调用的。现在我明白问题出在哪里了,但据我所知,我以前从未遇到过。这是正常的吗?有没有办法避免这种行为?

标签: ios uiview uiviewcontroller


【解决方案1】:

我认为您的问题在于,在您设置的-(void)viewWillLayoutSubviews 方法中,假设您的视图的默认框架,如果您在视图上调用 setFrame 后尝试在其他方法中更改框架,-(void)viewWillLayoutSubviews 也将被调用,并且视图的框架将是默认的。尝试从您的-(void)viewWillLayoutSubviews 中删除 setFrame。

【讨论】:

    【解决方案2】:

    您的视图控制器是否设置在情节提要中,并且您是否使用自动布局(默认情况下启用?)如果是这样,setFrame 将不起作用,您需要在情节提要中为它们创建出口后编辑约束。

    或者,您可以在情节提要中关闭自动布局,如 here 所示。

    【讨论】:

    • 你好,我没有使用故事板。
    • 即使我不使用情节提要,自动布局是否默认开启?
    • 故事板或 xibs,从 ios6 开始。如果你不使用它,我的回答是没用的。
    • 伙计,你的规则! .xib 中的视图超级视图在 ^_^ 上有“自动调整子视图”标志
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-14
    相关资源
    最近更新 更多