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