[英]SubView hierarchies not working (SubView hidden below another)
我在使用Xcode设计UI时遇到问题。
我已经以编程方式创建了(按下按钮时)UIImageView(黑色,透明的覆盖层),UITextView(可编辑)和UIButton来清除前两个UISubviews。
这个问题同时出现在模拟器和我的iPhone上。
这是我的代码
- (IBAction)openCommentTextView:(id)sender {
doneButton = [[UIButton alloc] initWithFrame:CGRectMake(20, 40, 60, 35)];
doneButton.alpha = 0.0;
doneButton.layer.cornerRadius = 7;
doneButton.layer.borderColor = [[UIColor colorWithWhite:1.0 alpha:1.0] CGColor];
doneButton.layer.borderWidth = 3;
[doneButton setTitle:@"Done" forState:UIControlStateNormal];
[doneButton setTitleColor:[UIColor colorWithWhite:1.0 alpha:1.0] forState:UIControlStateNormal];
doneButton.titleLabel.font = [UIFont fontWithName:@"AvenirNext-Regular" size:15];
[doneButton addTarget:self action:@selector(finishEditing:) forControlEvents:UIControlEventTouchUpInside];
commentTextView = [[UITextView alloc] initWithFrame:CGRectMake(20, 90, 280, 230)];
[commentTextView becomeFirstResponder];
commentTextView.alpha = 0.0;
if ([commentsTextViewViewer.text isEqual: @"Comment..."]) {
commentsTextViewViewer.text = @"";
} else {
commentTextView.text = commentsTextViewViewer.text;
}
commentTextView.keyboardAppearance = UIKeyboardAppearanceDark;
commentTextView.layer.cornerRadius = 7;
commentTextView.font = [UIFont fontWithName:@"GillSans" size:20];
commentTextView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:0.4];
backgroundOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 330, 568)];
backgroundOverlay.backgroundColor = [UIColor colorWithWhite:0.0 alpha:1.0];
backgroundOverlay.alpha = 0.0;
[self.view insertSubview:doneButton atIndex:11];
[self.view insertSubview:commentTextView atIndex:10];
[self.view insertSubview:backgroundOverlay atIndex:9];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
commentTextView.alpha = 1.0;
doneButton.alpha = 1.0;
[UIView commitAnimations];
[UIView setAnimationDuration:0.3];
backgroundOverlay.alpha = 1.0;
[UIView commitAnimations];
}
但是,UIButton(doneButton)和UITextView(commentTextView)不知何故位于UIImageView(黑色覆盖)之下。 UIButton和UITextView都响应。 (我将UIImageView(黑色覆盖)alpha设置为1.0
,虽然都响应了,但UIButton和UITextView没有出现)
原来是这样的 (对不起,我无法将图片直接包含在此处...我的声誉不高)
但是,当我复制此代码并将其粘贴到新的单视图应用程序中时,它可以按我希望的那样完美运行。 (像这样)
我真的不知道为什么会这样...我已经在网上搜索了所有内容,但都没有成功。
这与XCode或我的iPhone本身有关吗?
如果有人可以帮助我解决这个问题,我将非常高兴。
谢谢。
有几件事可能会有所帮助。 除了索引0
之外,我在atIndex:
视图插入方法方面从未有过多少运气。 尝试使用相对位置,因为您知道按钮和textview需要在背景叠加层上方。
[self.view addSubview:backgroundOverlay];
[self.view insertSubview:doneButton aboveSubview:backgroundOverlay];
[self.view insertSubview:commentTextView aboveSubview:backgroundOverlay];
另外,您需要再次调用[UIView beginAnimations:nil context:nil];
在动画第二段代码之前。 或者,如果您的目标是iOS 4或更高版本,则建议使用UIView动画块 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.