【问题标题】:SubView hierarchies not working (SubView hidden below another)SubView 层次结构不起作用(SubView 隐藏在另一个下方)
【发布时间】:2014-08-02 18:22:36
【问题描述】:

我在 Xcode 中设计 UI 时遇到问题。

我以编程方式(当按下按钮时)创建了一个 UIImageView(黑色,透明覆盖)、UITextView(可编辑)和一个 UIButton 来清除这两个 UISubview。

这个问题都出现在模拟器和我的 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(black overlay) alpha 设置为1.0,UIButton 和 UITextView 都没有出现,虽然都响应了)

原来是this(对不起,我无法将图片直接包含在此处...我没有足够的声誉)

但是当我复制这段代码并粘贴到一个新的单视图应用程序中时,它可以完美地工作,就像我希望的那样。 (like this)

我真的不知道为什么会发生这种情况......我已经在整个网络上搜索但没有成功。

这与 XCode 或我的 iPhone 本身有关吗?

如果有人能帮我解决这个问题,我会很高兴。

谢谢。

【问题讨论】:

    标签: iphone objective-c uiimageview uibutton xcode5


    【解决方案1】:

    有几件事可能会有所帮助。除了索引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 animation blocks

    【讨论】:

    • 是的,我终于修好了。我想知道为什么atIndex: 不起作用。
    • 另外,是的,我也必须修复动画,我只是忘记了。而且我以前也用块来做 UIView 动画,但是不知怎么的,动画卡住了,不流畅,所以我不得不这样写。但是感谢您的所有建议!
    • @CloudyLily0009 这可能是因为索引 9、10 和 11 超出了子视图数组的范围。在这种情况下,它的行为类似于addSubview,只是将它们附加到末尾。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-10
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多