在设备方向上设置UITextVIew位置



我将UITextView定位在屏幕中央,无论我是在横向还是纵向位置(iPhone或iPad),它都能正常工作。问题是,当我旋转设备时,它不会停留在中心。我以为它会自动保持在中心。

由于文本视图是在viewWillAppear中创建的,我想知道是否有一种方法可以在设备方向上刷新视图。或者可能只是为了转移视线。

我下面的代码:

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    UITextView *infoTextView = [[UITextView alloc] init];
    infoTextView.frame = CGRectMake(0, 0, 300.0, 300.0);
    infoTextView.center = CGPointMake(CGRectGetWidth(self.view.bounds)/2.0f, CGRectGetHeight(self.view.bounds)/2.0f);
    infoTextView.backgroundColor = [UIColor clearColor];
    NSString *anotherViewText = @"This is the content";
    infoTextView.dataDetectorTypes = UIDataDetectorTypeLink;
    infoTextView.text = anotherViewText;
    [infoTextView setFont:[UIFont fontWithName:@"ArialMT" size:18]];
    infoTextView.editable = NO;
    infoTextView.textAlignment=NSTextAlignmentCenter;
    [self.view addSubview:infoTextView];  
}

在UIView的情况下,在layoutSubviews中布局子视图是一种很好的做法,在UIViewController的情况下在viewDidLayoutSubviews中布局。所以在你的情况下,你可以简单地做:

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];
  self.infoTextViewCenter = self.view.center;
}

That's assuming you have infoTextView on a property.

Alternative and better approach is to use Autolayout and add horizontal and vertical position constraints to your view.

Another solution is just to set constraints on your textView and tell it to be vertically and horizontally aligned on your superview.

Replace the code with this:

UITextView *infoTextView = [[UITextView alloc] init];
infoTextView.frame = CGRectMake(0, 0, 300.0, 300.0);
infoTextView.center = self.view.center;
infoTextView.autoresizingMask = UIViewAutoresizingNone;
infoTextView.backgroundColor = [UIColor clearColor];
NSString *anotherViewText = @"This is the content";
infoTextView.dataDetectorTypes = UIDataDetectorTypeLink;
infoTextView.text = anotherViewText;
[infoTextView setFont:[UIFont fontWithName:@"ArialMT" size:18]];
infoTextView.editable = NO;
infoTextView.textAlignment=NSTextAlignmentCenter;
[self.view addSubview:infoTextView];

在代码中使用这些方法。并在-(void)landScapeFrame中为您编写横向代码,在-(虚空)potraitFrame 中编写纵向代码

#pragma mark - Auto rotate methods
#pragma mark for pre - iOS 6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
BOOL returnval=NO;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    if(interfaceOrientation==UIInterfaceOrientationPortrait||interfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
    {
        [self potraitFrame];
    }
    else
    {
        [self landScapeFrame];
    }
    returnval = YES;
}
else
{
    [self landScapeFrame];
    returnval= (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight);
}
return returnval;
}
 #pragma mark for - iOS 6
 -(BOOL)shouldAutorotate
 {
return YES;
 }
 -(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation  duration:(NSTimeInterval)duration {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
           if(toInterfaceOrientation==UIInterfaceOrientationPortrait||toInterfaceOrientation==UIInterfaceOrientationPortraitUpsideDown)
    {
        [self potraitFrame];
    }
    else
    {
        [self landScapeFrame];
    }
}
else
{
    [self landScapeFrame];
}

}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
 UIInterfaceOrientationLandscapeLeft;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    return self.interfaceOrientation;
}
else
{
    UIInterfaceOrientation crntOrntatn = self.interfaceOrientation;
    return UIInterfaceOrientationIsLandscape(crntOrntatn) ? crntOrntatn : UIInterfaceOrientationLandscapeLeft;
}

}

 -(NSUInteger)supportedInterfaceOrientations
 {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
    return UIInterfaceOrientationMaskAll;   
}
else
{
    return UIInterfaceOrientationMaskLandscape;
}
}

最新更新