问题描述
以编程方式向视图添加 UIProgressView 会忽略 size.height 值,这显然是因为 NSAutoresizingMaskLayoutConstraint 始终为 2.除了增加高度之外,关于 UIProgressView 的其他一切都是标准的.progressViewFrame.size.height = 40.0 的代码片段:
Adding a UIProgressView to a view programatically ignores the size.height value, apparently due to NSAutoresizingMaskLayoutConstraint always being 2. Other than increasing the height, everything else about the UIProgressView is standard. A code snippet where progressViewFrame.size.height = 40.0:
self.progressView = [[UIProgressView alloc] init];
// initWithFrame produces the same issue
[self.progressView setFrame:progressViewFrame];
[self addSubview:self.progressView];
导致标准高度为 2.
强制附加高度 40.0 约束:
Forcing an additional height 40.0 constraint:
[self addConstraint:[NSLayoutConstraint
constraintWithItem: self.progressView
attribute: NSLayoutAttributeHeight
relatedBy: NSLayoutRelationEqual
toItem: nil
attribute: NSLayoutAttributeNotAnAttribute
multiplier: 1.0
constant: 40.0]];
通过报告以下(预期的)冲突来说明问题:
illustrates the issue by reporting the following (expected) conflict:
(
"<NSLayoutConstraint:0x16d74b70 V:[UIProgressView:0x16d72800(40)]>",
"<NSAutoresizingMaskLayoutConstraint:0x16d69e60 h=--& v=--& V:[UIProgressView:0x16d72800(2)]>"
)
这似乎与在 IB 中使用 40.0 高度约束创建更高的 UIProgressView 相关,该约束生成预期:高度 = 40 实际:高度 = 2 警告(但显示正确).
This appears to be related to creating the taller UIProgressView in IB with a 40.0 height constraint which generates an Expected: height=40 Actual: height=2 warning (but displays correctly).
这些问题在 iOS 7.1 中出现,我正在为上面的程序化 UIProgressView 案例寻找解决方案.我不担心 7.0 之前的 iOS 版本,我希望尽可能少地围绕标准的默认 UIProgressView 进行 hack.(我只是想让它更高.)
These issues appeared with iOS 7.1, and I'm looking for a workaround solution for the programmatic UIProgressView case above. I'm not worried about iOS versions before 7.0, and I'm hoping to hack as little as possible around the standard, default UIProgressView. (I just want it taller.)
预先感谢您的任何建议.
Thanks in advance for any suggestions.
推荐答案
看来唯一可行的策略是完全使用 LayoutConstraints 来消除问题.这是我最终使用的内容(感谢 John Griffith 的建议):
It appears the only feasible strategy is to go entirely to LayoutConstraints to eliminate the problem. Here is what I ended up using (thanks to John Griffith for the advice):
self.progressView = [[UIProgressView alloc] init];
self.progressView.translatesAutoresizingMaskIntoConstraints = NO;
[self addSubview: self.progressView];
NSDictionary *views = @{@"progressview": self.progressView};
NSDictionary *metrics = @{@"height": @(progressBarHeight),
@"margin": @(horizontalMargin),
@"bottomspace": @(bottomMargin)};
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat: @"H:|-(margin)-[progressview]-(margin)-|"
options: 0
metrics: metrics
views: views]];
[self addConstraints:[NSLayoutConstraint
constraintsWithVisualFormat: @"V:[progressview(height)]-(bottomspace)-|"
options: 0
metrics: metrics
views: views]];
这篇关于以编程方式创建的 UIProgressView 的高度在 iOS 7.1 中被忽略(总是 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!