ios - setFrame 不会做任何事情

标签 ios uiview uiviewcontroller

我有一个应用程序不会在 -init 和 -viewWillLayoutSubviews 方法之外设置框架的问题。当点击 editButton 时应该发生的是一个隐藏编辑器 View 的动画。尽管如此,当我测试它时没有任何反应。问题不是来自动画方法,因为 -setFrame 方法(未包含在 block 中)也不起作用。

代码如下:

-(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]];


}

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

最佳答案

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

关于ios - setFrame 不会做任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16220531/

相关文章:

ios - 使用 UIGestureRecogniser 子类化时未调用 Touches Ended

ios - 由于 AVPlayer 和观察者的初始化失败导致内存泄漏

swift - 符合协议(protocol)但没有任何反应

ios - 有没有办法知道我的应用目前有多少安装量?

ios - 如何在 Socket.io Swift4 中发出事件

iphone - iOS - 无法访问 didSelectRowAtIndexPath 中的 NSMutableArray?

iOS 13 全屏 UIImagePickerController?

ios - Flutter - TextFormField - 无法同时满足约束(NSAutoresizingMaskLayoutConstraint)

ios - 键盘打开时更改 View 位置 - objective-c

iphone - 顺序 UIView 变换