我目前正在使用带有textview的警报视图,但是当我将iPhone旋转到横向时会发生奇怪的事情,另一个文本视图似乎出现在警报视图中。这个问题不会发生在iPad上。有谁知道是什么原因引起的?任何建议或帮助将不胜感激。
我的代码放在app delegate中,列在下面,
- (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
答案 0 :(得分:1)
初始化UIAlertView时只需要更改'message'属性:
showSelection = [[UIAlertView alloc]
initWithTitle: @""
message: @""
delegate: nil
cancelButtonTitle: @"OK"
otherButtonTitles: nil];
请注意,如果要调整UIAlertView的大小,可以在视图控制器中使用以下代码:
- (void) willPresentAlertView:(UIAlertView *)alertView
{
alertView.frame = CGRectMake(0, 0, 150, 250);
}
答案 1 :(得分: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];
}
答案 2 :(得分: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);
}
}
答案 3 :(得分:0)
您有3个选项可以解决此问题:
我在项目中遇到了同样的问题,当时我选择了第二个选项。