我有一个视图控制器,需要在横向和纵向之间进行不同的布局。除了一件小事之外,以下代码几乎完美无缺。对于移动的按钮(标签= 3的按钮),当我单击另一个按钮(其标签= 0并调用(IBAction)myButton :)时,按钮移动到放置它的位置在故事板中。标签不移动,只是按钮。只有在(IBAction)myButton中设置的标题值与按下按钮之前的值不同时才会发生这种情况。
- (IBAction)myButton:(UIButton *)sender {
UIButton *but2 = (UIButton * )[self.view viewWithTag:3];
[but2 setTitle: @"A" forState:UIControlStateNormal];
UILabel *lab =(UILabel *)[self.view viewWithTag:2];
lab.text=@"B";
}
-(void)rotateViewToPortrait{
UIButton *but2 = (UIButton * )[self.view viewWithTag:3];
[but2 setFrame:CGRectMake(100, 500, but2.frame.size.width, but2.frame.size.height)];
UILabel *lab =(UILabel *)[self.view viewWithTag:2];
[lab setFrame:CGRectMake(100,550 , lab.frame.size.width, lab.frame.size.height)];
}
-(void)rotateViewToLandscape{
UIButton *but2 = (UIButton * )[self.view viewWithTag:3];
[but2 setFrame:CGRectMake(500, 100 , but2.frame.size.width, but2.frame.size.height)];
UILabel *lab =(UILabel *)[self.view viewWithTag:2];
[lab setFrame:CGRectMake(500,150 , lab.frame.size.width, lab.frame.size.height)];
}
-(void)viewDidAppear:(BOOL)animated{
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
if ((orientation==UIInterfaceOrientationLandscapeRight)||(orientation==UIInterfaceOrientationLandscapeLeft)) {
[self willAnimateRotationToInterfaceOrientation:UIInterfaceOrientationLandscapeRight duration:0];
} else {
[self willAnimateRotationToInterfaceOrientation:UIInterfaceOrientationPortrait duration:0];
}
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
if ((toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)||(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft)) {
[self rotateViewToLandscape];
} else if ((toInterfaceOrientation == UIInterfaceOrientationPortrait)||(toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)){
[self rotateViewToPortrait];
}
}
答案 0 :(得分:0)
我通过关闭故事板中视图控制器中的AutoLayout来解决问题。我还删除了所有弹簧和支柱,并在主视图中关闭了Autoresize子视图。 我仍然不确定为什么行为如上所述。但嘿嘿它现在有效。
答案 1 :(得分:0)
我遇到了类似的问题。
此代码:
if (howManyButtons==2) {
button1.hidden=true;
button2.hidden=false;
[button2 setFrame:CGRectMake(177,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)];
button3.hidden=false;
[button3 setFrame:CGRectMake(703,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)];
button4.hidden=true;
} else {
button1.hidden=false;
[button1 setFrame:CGRectMake(71,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)];
button2.hidden=false;
[button2 setFrame:CGRectMake(317,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)];
button3.hidden=false;
[button3 setFrame:CGRectMake(563,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)];
button4.hidden=false;
[button4 setFrame:CGRectMake(809,button2.frame.origin.y,button2.frame.size.width,button2.frame.size.height)];
}
当(howManyButtons == 2)时,如果按钮上的图像加载了这样的代码:
UIImage *btnImage4 = [UIImage imageWithContentsOfFile:path];
[button4 setImage:btnImage4 forState:UIControlStateNormal];
按钮按预期移动,但当按钮上的图像加载了此代码时(从iPad的照片库而不是应用程序本身的资源获取图像):
NSURL *testURL = [[NSUserDefaults standardUserDefaults] URLForKey:[self albumNameForPic:albumNo picNo:[self picNoForAlbum:albumNo picNo:picNo]]];
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
[assetsLibrary assetForURL:testURL resultBlock: ^(ALAsset *asset){
CGImageRef imageRef = [asset thumbnail];
if (imageRef) {
switch (buttonNo) {
case 1:
[button1 setImage:[UIImage imageWithCGImage:imageRef scale:0.5 orientation:UIImageOrientationUp] forState:UIControlStateNormal];
break;
case 2:
[button2 setImage:[UIImage imageWithCGImage:imageRef scale:0.5 orientation:UIImageOrientationUp] forState:UIControlStateNormal];
break;
case 3:
[button3 setImage:[UIImage imageWithCGImage:imageRef scale:0.5 orientation:UIImageOrientationUp] forState:UIControlStateNormal];
break;
case 4:
[button4 setImage:[UIImage imageWithCGImage:imageRef scale:0.5 orientation:UIImageOrientationUp] forState:UIControlStateNormal];
break;
default:
break;
}
}
} failureBlock:^(NSError *error) {
// Handle failure.
}];
按钮没有移动,它们停留在Main.storyboard中。
我猜测行为的差异取决于设置按钮图像的资源库代码是异步运行的事实......
关闭AutoLayout解决了这个问题但是,像大力水手一样,我不明白为什么(我不喜欢)。