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