Xcode:frame.size.height 变化使视图的高度向下变化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10467888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:12:00  来源:igfitidea点击:

Xcode: frame.size.height change makes view's height change in the downward direction?

iphonexcodeviewheight

提问by davis

I'm trying to have a bar that fluctuates with the mic level, but am having trouble getting it to fluctuate in the upwards direction. Here's the code

我试图有一个随麦克风电平波动的条,但我无法让它向上波动。这是代码

- (void)setVUMeterHeight:(float)height {
     if (height < 0)
        height = 0;

     CGRect frame = vuMeter.frame;
     frame.size.height = height+10;
     vuMeter.frame = frame;
}

- (void)updateVUMeter {
float height = (90+voiceSearch.audioLevel)*5/2;

     [self setVUMeterHeight:height];    
     [self performSelector:@selector(updateVUMeter) withObject:nil afterDelay:0.05];
}

The problem is the height starts at 10px, and when the mic becomes active, the height is extended, but in the downward direction. I tried to fix this by giving the view negative height values, but views can't have negative heights. Does anyone have an idea for how to get the height to change in the upward direction? Thanks.

问题是高度从 10px 开始,当麦克风处于活动状态时,高度会延伸,但方向是向下的。我试图通过给视图负高度值来解决这个问题,但视图不能有负高度。有没有人知道如何使高度向上改变?谢谢。

回答by EricS

Extend the origin upwards by the same amount you extend the size.

将原点向上扩展与扩展大小相同的量。

Something like:

就像是:

- (void)setVUMeterHeight:(float)height {
     if (height < 0)
        height = 0;

     const CGFloat kMeterBottom = 200;
     CGRect frame = vuMeter.frame;
     frame.size.height = height+10;
     frame.origin.y = kMeterBottom - frame.size.height;
     vuMeter.frame = frame;
}