2013-12-15 40 views
0

我从教程系列中使用以下滚动视图。它可以在纵向模式下正常工作,但帧不会处理切换到横向模式。有没有快速的方法来解决这个问题?提前致谢。风景的iOS scrollview自适应

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    pageControlBeingUsed = NO; 
    NSString *plistFile = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", self.displayRegion] ofType:@"plist"]; 
    self.SituationData = [NSArray arrayWithContentsOfFile:plistFile]; 

    self.singleSituation = [SituationData objectAtIndex:selectedIndexPath]; 

    self.SituationScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)]; //distinguishing between iphone 4 and 5 screen 

    self.SituationScrollView.delegate = self; 
    self.SituationScrollView.showsHorizontalScrollIndicator = NO; 
    self.SituationScrollView.pagingEnabled = YES; 
    self.SituationScrollView.backgroundColor = [UIColor clearColor]; 

    //adding to the view 
    [self.view addSubview:self.SituationScrollView]; 

    //getting the base indicator 
    int baseNum = self.selectedIndexPath ; 

    self.plistFile = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", self.displayRegion] ofType:@"plist"]; 
    self.auswahl = [NSArray arrayWithContentsOfFile:self.plistFile]; 

    self.SituationScrollView.contentSize = CGSizeMake(self.SituationScrollView.frame.size.width *[self.auswahl count], self.SituationScrollView.frame.size.height); 

    for (NSInteger i=1; i <= [[self auswahl] count]; i++){ 

     UIImage *situationImage = [UIImage imageNamed:[NSString stringWithFormat:@"main%@", [[self.auswahl objectAtIndex:i - 1] objectForKey:@"nat_num"]]]; 
     UIImageView *situationView = [[UIImageView alloc] initWithImage:situationImage]; 

     UILabel *situationTitle = [[UILabel alloc] init]; 
     situationTitle.text = [NSString stringWithFormat:@"%@", [[self.auswahl objectAtIndex: i - 1] objectForKey:@"species"]]; 
     [situationTitle setTextColor:[UIColor blackColor]]; 
     [situationTitle setBackgroundColor:[UIColor clearColor]]; 

     UITextView *description = [[UITextView alloc] init]; 
     description.text = [NSString stringWithFormat:@"%@", [[self.auswahl objectAtIndex: i - 1] objectForKey:@"description"]]; 
     [description setTextColor:[UIColor blackColor]]; 
     [description setBackgroundColor:[UIColor clearColor]]; 
     [description setUserInteractionEnabled:NO]; 


     situationView.frame = CGRectMake((320 * (i-1)) + SIT_IMG_X, 66, 94, 87); 
     situationTitle.frame = CGRectMake((320 * (i-1)) + TOP_DATA_X, 250, 240, 14); 
     description.frame = CGRectMake((320 * (i-1)) + DESC_X, DESC_Y, DETAIL_WIDTH, 100); 


     [self.SituationScrollView addSubview:situationView]; 
     [self.SituationScrollView addSubview:situationTitle]; 
     [self.SituationScrollView addSubview:description]; 

    } 

回答

0

小googlefu揭示this SO thread其中麦克各州加载的UIScrollView在viewWillAppear方法而不是viewDidLoad方法,则如所期望的的UIScrollView应该出现。

1
use a global BOOL variable 

BOOl isLandscape = NO;

while writing frames 

    situationView.frame = CGRectMake(isLandscape?0:0, isLandscape?0:0, isLandscape?320:480 , isLandscape?480:320); 

定向方法写一遍

if(orientation == Landscape) 

{ 

isLandscape=YES; 

} 

else 

{ 

isLandscape=NO; 

} 

    situationView.frame = CGRectMake(isLandscape?0:0, isLandscape?0:0, isLandscape?320:480 , isLandscape?480:320); 

检查它,因为它会工作取决于它的样本可以使用