我将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];
}
答案 0 :(得分:1)
最好在UIView的情况下在layoutSubviews
中布局子视图,在UIViewController的情况下在viewDidLayoutSubviews
。
所以在你的情况下,你可以简单地做:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.infoTextViewCenter = self.view.center;
}
假设你在一个属性上有infoTextView。
替代方案和更好的方法是使用Autolayout并在视图中添加水平和垂直位置约束。
答案 1 :(得分:1)
另一种解决方案是在textView上设置约束,并告诉它在超级视图上垂直和水平对齐。
答案 2 :(得分:0)
用以下代码替换代码:
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];
答案 3 :(得分:0)
在代码中使用这些方法。并在 - (void)landScapeFrame中写下横向代码并在 - (void)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;
}
}