在方向更改上以编程方式更新UI

时间:2014-08-11 17:08:08

标签: ios objective-c

我想在用户更改设备的方向时更新UI内容。 我使用以下代码在-viewDidLoad事件创建一个UITableView。

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)    name:UIDeviceOrientationDidChangeNotification  object:nil];

    self.tv = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.tv.backgroundColor = [UIColor redColor];
    self.tv.autoresizesSubviews = YES;
    self.tv.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
    self.tv.delegate = self;
    self.tv.dataSource = self;
    [self.view addSubview:self.tv];

}

我使用以下事件处理方向更改但我无法将tableview缩放到屏幕。 你能帮我找一下我错过的东西吗?

#pragma mark - orientation events

-(BOOL)shouldAutorotate
{
    return YES;
}

- (void)orientationChanged:(NSNotification *)notification{
    [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}

-(void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {


    [self.tv reloadData];
    if( orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown){

    }
    if( orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) {

    }
}

0 个答案:

没有答案