问题
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];
results in the standard height of 2.
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)]>"
)
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).
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.
回答1:
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]];
来源:https://stackoverflow.com/questions/22645533/height-of-programmatically-created-uiprogressview-is-ignored-in-ios-7-1-always