Closed Brandon-T closed 7 years ago
You don't need to constrain the size of the content view: it is automatically sized to match the shimmer view, through the lines of code you showed above.
Does it work without that constraint?
No it doesn't work without that constraint. Instead it doesn't display at all.
With FBShimmeringView: http://imgur.com/50W0YOc Without FBShimmeringView: http://imgur.com/v2hB2Qw
I created a sample test project to show the issue: https://github.com/Brandon-T/FBShimmerTest/tree/master/FacebookShimmerTest
In the LoadingCell
class I added a macro #define kDisableFBShimmering
. If it is defined, then you can see everything works perfectly. The views get displayed. Console does not log any errors.
However, when I comment it out (enable FBShimmering), you can see the console spitting out a lot of errors and nothing gets disabled on screen.
I took a look in the Xcode view debugger, and it looked like the bottom view is positioned and sized correctly. The issue is the subviews of the bottom view. I remembered that autolayout requires geometry methods to be called on UIView
, rather than CALayer
, as Shimmer is currently implemented.
Changing that makes the views appear, but there's still log messages about unsatisfied constraints. Investigating those, it appears the issue is that your constraints setup in BottomView
require a certain minimum width and height of the BottomView
to resolve. In a pure-autolayout world, that's OK, because that requirement is factored into constraint resolution. But Shimmer's manual sizing of the BottomView
happens after the autolayout pass, so the BottomView
needs a sufficient initial size.
In summary, the two fixes to make this work:
In your LoadingCell.m
, right after creating a BottomView
, give it an initial size. Note that the size is arbitrary, as long as it's as big as BottomView
's layout requires, and that this can't use -initWithFrame:
because BottomView
overrides the convenience initializer -init
instead.
self.bottomView.frame = CGRectMake(0, 0, 500, 1000);
Alternatively, you could add the constraints in BottomView
later, after initialization, when the view has a correct size.
In Shimmer's FBShimmeringView.m
, add this method to set the frame of the view instead of the layer:
- (void)layoutSubviews
{
// Autolayout requires these to be set on the UIView, not the CALayer.
// Do this *before* the layer has a chance to set the properties, as the
// setters would be ignored (even for autolayout) if set to the same value.
_contentView.bounds = self.bounds;
_contentView.center = self.center;
[super layoutSubviews];
}
This is pushed to master as 3589bd7ca0d122fe06792868485d79c622a3f47d.
This works. However, you cannot use UITableViewAutomaticDimension
with the above. For example, if you constrained [shimmerView.heightAnchor constraintGreaterThanOrEqualToConstant:50.0].active = YES
and made the UITableView height dynamic, you will get more constraint problems. However if you did the same to bottomView
, it will work just fine.
I will be using a static height anyway so it doesn't matter, but I just thought I'd mention it in case someone came along with the same issue.
I created a UIView. Added it as contentView of the shimmerView.
shimmerView is constrained to my TableViewCell.contentView -- left, right, top, bottom. The UIView is then constrained to shimmerView -- left, right, top, bottom.
[shimmerView setShimmering:YES]; then gives the error:
If I go to
FBShimmeringLayer
layoutSubViews and comment out:the errors go away but Shimmering no longer works.