简体   繁体   English

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

[英]iOS UIView Background Becomes Transparent When Moved

Right now, I'm using autolayout to update the constraints of this view so that it gets shifted upwards when the keyboard shows. 现在,我正在使用自动布局更新此视图的约束,以便在键盘显示时它会向上移动。 Here is the method: 方法如下:

- (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];
    }
}

It works fine, but for some reason, whenever the keyboard appears, the background of the view becomes transparent. 它可以正常工作,但是由于某些原因,只要出现键盘,视图的背景就会变得透明。

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

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

Any ideas why this is happening? 任何想法为什么会这样?

Here is a picture of my constraints: http://imgur.com/YlfzwhA 这是我的约束的图片: http : //imgur.com/YlfzwhA

Even weirder behavior! 甚至更怪异的行为! Take a look at this - if I move the starting position of the view like in this picture: http://imgur.com/0XKtLC1 看看这个-如果我像这样移动图片的开始位置: http : //imgur.com/0XKtLC1

Then when it moves up, only half the view seems to show up. 然后,当它向上移动时,似乎只显示一半的视图。 What could this mean? 这意味着什么? http://imgur.com/dYddOLK http://imgur.com/dYddOLK

First reason: Your problem is in your notification method 第一个原因:您的问题出在您的通知方法中

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

Sender is a uitextfield or uitextview and you set frame of yelpSearchView. 发件人是uitextfield或uitextview,您可以设置yelpSearchView的框架。 I think the yelpSearchView background color is Clearcolor. 我认为yelpSearchView背景色是Clearcolor。

Second reason: In your design may have a problem. 第二个原因:在您的设计中可能有问题。 I see your constraints picture. 我看到您的约束图。 just Put the search bar in a view and set frame of that view 只需将搜索栏放在一个视图中并设置该视图的框架

Try to add height constraint for your view. 尝试为视图添加高度限制。 I suppose, it don't clip subviews and height = 0. 我想,它不会剪辑子视图且height = 0。

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

This was my problem! 这是我的问题! I have no idea why I was setting the bottom constraint to this - I only needed to set it to the frame.size.height. 我不知道为什么要为此设置底部约束-我只需要将其设置为frame.size.height。 I was adding too much extra, so the view was getting pushed past the visible area. 我添加了太多额外的内容,因此视图被推到可见区域之外。 I changed it to 我将其更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM