以编程方式更改NSProgressIndicator的高度

时间:2014-09-14 12:58:59

标签: objective-c macos cocoa

我找不到以编程方式设置NSProgressIndicator高度的方法。

到目前为止我的尝试:

NSProgressIndicator *ind = [[NSProgressIndicator alloc] init];
[ind setStyle: NSProgressIndicatorBarStyle];
// Height does not change height of the actual indicator
[ind setFrame: NSMakeRect(0, 0, 100, 50)];
[ind setBounds: NSMakeRect(0, 0, 100, 50)];
//[ind setControlSize: 0]; does only make it smaller, not bigger
[view addSubview: ind];

我在文档中找到了NSProgressIndicatorBarStyle枚举,但是我找不到指定厚度的方法。

这是一个描述我的问题的屏幕截图:(图层背景为红色以便更好地理解):

enter image description here

使用NSButton类时也会发生这种情况。有解决方法吗?

2 个答案:

答案 0 :(得分:3)

iOS中的

由于框架限制,您无法更改进度指示器高度只是更改其框架。但是,您应该能够获得相同的结果,使用转换

_indicator.transform = CGAffineTransformMakeScale(1.0f, 0.6f);

编辑:我刚试过Mac OS X

_indicator.layer.transform = CATransform3DMakeScale(1.0f, 0.6f, 0.0f);

并且它不起作用,因此它不像iOS,可能是因为它在Cocoa上的实现方式(如Ken建议的那样)。 我设法改变高度的唯一方法是使用controlSize,但我认为它不适合您的需求(因为它不允许您指定点数)。

[_indicator setControlSize:NSMiniControlSize]; // or NSSmallControlSize

您应该可以通过继承NSProgressIndicator并覆盖drawRect来使用任意帧,此时我的建议是环顾四周寻找可以扩展供您使用的内容,例如这个 https://www.cocoacontrols.com/controls/lbprogressbar

答案 1 :(得分:-1)

在IB中你可以

  1. 选择您的NSProgressIndicator控件
  2. 实用程序视图中的
  3. 选择View Effects检查器
  4. 在内容过滤器中按+
  5. 选择Lanczos Scale Transform过滤器
  6. 在“比例”行中设置适当的比例值
  7. 如果您只需要更改高度,也可以设置宽高比
  8. 这也可以通过编程方式添加,只需谷歌如何将内容过滤器添加到NSView