0

我目前正在使用一个带有 textview 的警报视图,但是当我将 iPhone 旋转到横向时发生了一些奇怪的事情,另一个 textview 似乎出现在警报视图上。这个问题在 iPad 上不会发生。有谁知道这可能是什么原因造成的?任何建议或帮助将不胜感激。

我的代码放置在应用程序委托中,并在下面列出,

- (IBAction) loadAboutUs 
{
UIAlertView *showSelection;
showSelection.delegate = self;
NSString *key = [NSString stringWithFormat: @"Drug Tariff\n"
@"Version: %@\n"
@"text\n\n"
@"text\n"
@"text\n"
@"text", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
showSelection = [[UIAlertView alloc] 
                 initWithTitle: @""
                 message: @"\n\n\n\n\n\n\n\n\n\n"//The new lines are used to increase the size of the pop-up
                 delegate: nil 
                 cancelButtonTitle: @"OK" 
                 otherButtonTitles: nil];

//The text view inserted into the alertview 
UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(12.0, 15.0, 260.0, 210.0)];
myTextView.text = key;
myTextView.dataDetectorTypes = UIDataDetectorTypeAll;//set the textview to pick up all link types
[myTextView setFont:[UIFont fontWithName:@"ArialMT" size:16]];
myTextView.textAlignment = UITextAlignmentCenter; 
myTextView.editable = false;//the textview needs to be uneditable for the dataDectector to work
myTextView.scrollEnabled = true;
[myTextView setBackgroundColor:[UIColor whiteColor]];
[showSelection addSubview:myTextView];

[showSelection show];
}

我附上了这个问题的图片,

警报视图问题

更新:

当我运行下面的代码时得到的输出,

NSArray *subviews = [showSelection subviews];
for (UIView *view in subviews)
{
    NSLog(@"class %@, tag: %i", [view class], view.tag);
} 

Landscape
2012-06-18 17:56:43.906 TS Data[6513:b903] class UIImageView, tag: 0
2012-06-18 17:56:43.907 TS Data[6513:b903] class UILabel, tag: 0
2012-06-18 17:56:43.908 TS Data[6513:b903] class UILabel, tag: 0
2012-06-18 17:56:43.908 TS Data[6513:b903] class UIThreePartButton, tag: 1
2012-06-18 17:56:43.909 TS Data[6513:b903] class UITextView, tag: 1000
2012-06-18 17:56:43.910 TS Data[6513:b903] class UIAlertTextView, tag: 12345
2012-06-18 17:56:43.910 TS Data[6513:b903] class UIImageView, tag: 3334

Portrait
2012-06-18 17:56:38.035 TS Data[6513:b903] class UIImageView, tag: 0
2012-06-18 17:56:38.053 TS Data[6513:b903] class UILabel, tag: 0
2012-06-18 17:56:38.054 TS Data[6513:b903] class UILabel, tag: 0
2012-06-18 17:56:38.055 TS Data[6513:b903] class UIThreePartButton, tag: 1
2012-06-18 17:56:38.055 TS Data[6513:b903] class UITextView, tag: 1000
4

4 回答 4

1

您只需在初始化 UIAlertView 时更改“消息”属性:

showSelection = [[UIAlertView alloc] 
             initWithTitle: @""
             message: @""
             delegate: nil 
             cancelButtonTitle: @"OK" 
             otherButtonTitles: nil];

请注意,如果要调整 UIAlertView 的大小,可以在视图控制器中使用以下代码:

- (void) willPresentAlertView:(UIAlertView *)alertView
{
    alertView.frame = CGRectMake(0, 0, 150, 250);
}
于 2012-07-26T07:13:37.833 回答
0

添加:

myTextView.tag = 1000 //number here does not matter as long as it is not 0

然后在您初始化 myTextView 之前,只需检查带有标签 1000 的视图是否存在:

if (![showSelection viewWithTag:1000]){//if that view is not there add it, if not, it is there ;)
 UITextView *myTextView = [[UITextView alloc] initWithFrame:CGRectMake(12.0, 15.0, 260.0, 210.0)];
 myTextView.tag = 1000
 myTextView.text = key;
 myTextView.dataDetectorTypes = UIDataDetectorTypeAll;//set the textview to pick up all link types
 [myTextView setFont:[UIFont fontWithName:@"ArialMT" size:16]];
 myTextView.textAlignment = UITextAlignmentCenter; 
 myTextView.editable = false;//the textview needs to be uneditable for the dataDectector to work
 myTextView.scrollEnabled = true;
 [myTextView setBackgroundColor:[UIColor whiteColor]];
 [showSelection addSubview:myTextView];
}
于 2012-06-18T14:31:21.447 回答
0

在 iPhone 横向模式下,alertView 框架发生变化。我通过更改方向上的 textField 框架手动处理它。

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
   if (((orientation == UIInterfaceOrientationLandscapeLeft) || (orientation == UIInterfaceOrientationLandscapeRight)) && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone))
   {
        yourTextField.frame = CGRectMake(20.0, 32.0, 245.0, 25.0);
   }
   else
   {
        yourTextField.frame = CGRectMake(20.0, 45.0, 245.0, 25.0);
   }
}
于 2012-06-18T16:07:32.850 回答
0

您有 3 个选项来解决此问题:

  1. 根据旋转手动设置框架。就像“Warif Akhand Rishi”给出的答案
  2. 如果存在警报视图,则不允许视图旋转。(从 shouldAutoRotateToInterfaceOrientation 方法返回 NO)
  3. 界面变化时关闭警报视图,并在视图旋转后显示。它需要一点点代码。如果您的 textField 中有任何内容,那么您想暂时存储它并再次显示它。

我在我的项目中遇到了同样的问题,当时我选择了第二个选项。

于 2012-06-20T19:00:11.313 回答