iOS UIView背景在移动时变得透明

时间:2014-08-14 04:49:52

标签: ios objective-c uiview autolayout nslayoutconstraint

现在,我正在使用autolayout来更新此视图的约束,以便在键盘显示时向上移动。这是方法:

- (void)keyboardWillShow:(NSNotification *)sender
{
    if ( [self.yelpSearchTextField isFirstResponder] || [self.timeTextField isFirstResponder] || [self.radiusTextField isFirstResponder] ) {
        CGRect frame = [sender.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
        CGRect viewFrame = self.yelpSearchView.frame;
        self.keyboardHeightConstraint.constant = frame.size.height + viewFrame.size.height;
        [self.view layoutIfNeeded];
    }
}

它工作正常,但出于某种原因,每当键盘出现时,视图的背景变得透明。

之前:http://imgur.com/Axg3b9c

之后:http://imgur.com/WA1zgSL

为什么会发生这种情况?

这是我的约束的图片: http://imgur.com/YlfzwhA

甚至更奇怪的行为!看看这个 - 如果我移动视图的起始位置,如下图所示:http://imgur.com/0XKtLC1

然后当它向上移动时,只有一半的视图出现。这意味着什么? http://imgur.com/dYddOLK

3 个答案:

答案 0 :(得分:0)

第一个原因:您的问题出在通知方法

- (void)keyboardWillShow:(NSNotification *)sender;

Sender是一个uitextfield或uitextview,你可以设置yelpSearchView的框架。我认为yelpSearchView背景颜色是Clearcolor。

第二个原因:在您的设计中可能存在问题。 我看到了你的约束画面。 将搜索栏放在视图中并设置该视图的框架

答案 1 :(得分:0)

尝试为视图添加高度约束。我想,它不会剪辑子视图,高度= 0。

答案 2 :(得分:0)

self.keyboardHeightConstraint.constant = frame.size.height + viewFrame.size.height;

这是我的问题!我不知道为什么我要设置底部约束 - 我只需要将它设置为frame.size.height。我添加了太多的额外内容,因此视图被推到了可见区域。我把它改成了

self.keyboardHeightConstraint.constant = frame.size.height;