Убедитесь, что вид остается на весь экран при повороте - PullRequest
0 голосов
/ 06 декабря 2011

У меня есть простой UIWebView, который я добавил к своему UIViewController в методе viewDidLoad:

CGRect rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
self.webView = [[UIWebView alloc] initWithFrame:rect];
self.webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:self.webView];

И это выглядит великолепно, но когда я поворачиваю телефон, ширинаи высота остаются неизменными, так что теперь она слишком широка для рамки просмотра обновления.Я также попытался использовать self.view.bounds, но это не имело никакого значения.

Итак, как вы гарантируете, что полноэкранное представление при загрузке останется таким же размером при повороте?(без использования IB)

Ответы [ 2 ]

0 голосов
/ 06 декабря 2011

Поскольку веб-представление не вызывается только один раз, его необходимо вызвать снова для установки нового фрейма

self.webView = [[UIWebView alloc] initWithFrame:rect];

, поэтому вам необходимо зарегистрировать уведомление в viwewillappear или viewdidload

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewBecamePortrait:) name:@"orientationIsPortrait" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewBecameLandscape:) name:@"orientationIsLandscape" object:nil];



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation)) {
        NSNotification* notification = [NSNotification notificationWithName:@"orientationIsPortrait" object:self];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }else {
        NSNotification* notification = [NSNotification notificationWithName:@"orientationIsLandscape" object:self];
        [[NSNotificationCenter defaultCenter] postNotification:notification];
    }
}

Затемреализовать

-(void)viewBecameLandscape:(id)sender{
    if(webview){
      [webview.setframe(cgrectmake(x,y,width,height))];
    }
}
-(void)viewBecamePortrait:(id)sender{
}
0 голосов
/ 06 декабря 2011

То, что вы делаете, правильно и должно работать в большинстве сценариев.Но так как я понятия не имею о вашем стеке просмотра.Я предложу верный путь выстрела -

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
                                         duration:(NSTimeInterval)duration
{
    CGRect rect;
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft||toInterfaceOrientation == UIInterfaceOrientationLandscapeRight)
    {
       rect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }
    else
    {
        //some other dimensions.
    }
     self.webView = [[UIWebView alloc] initWithFrame:rect];
}
...