0

我有一个应用程序不会在 -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]];


}

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

4

2 回答 2

1

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

于 2013-04-25T18:07:28.577 回答
1

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

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

于 2013-04-25T17:04:55.720 回答