适用于风景的iOS scrollview

时间:2013-12-15 20:49:43

标签: ios uiscrollview landscape-portrait

我在教程系列中使用以下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];

    }

2 个答案:

答案 0 :(得分: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);

将其作为样本进行检查,具体取决于您可以使用

答案 1 :(得分:0)

有点googlefu透露this SO thread其中Mike声明在viewWillAppear方法而不是viewDidLoad方法中加载UIScrollView,然后UIScrollView应该按照需要显示。