1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #import "ios/web/public/web_state/ui/crw_generic_content_view.h"
7 #include "base/logging.h"
8 #include "base/mac/scoped_nsobject.h"
10 @interface CRWGenericContentView () {
11 // The size of the view's bounds at the last call to |-layoutSubviews|.
12 CGSize _lastLayoutSize;
13 // Backing objectect for |self.scrollView|.
14 base::scoped_nsobject<UIScrollView> _scrollView;
15 // Backing object for |self.view|.
16 base::scoped_nsobject<UIView> _view;
21 @implementation CRWGenericContentView
23 - (instancetype)initWithView:(UIView*)view {
24 self = [super initWithFrame:CGRectZero];
27 _lastLayoutSize = CGSizeZero;
28 _view.reset([view retain]);
29 _scrollView.reset([[UIScrollView alloc] initWithFrame:CGRectZero]);
30 [self addSubview:_scrollView];
31 [_scrollView addSubview:_view];
32 [_scrollView setBackgroundColor:[_view backgroundColor]];
37 - (instancetype)initWithCoder:(NSCoder*)decoder {
42 - (instancetype)initWithFrame:(CGRect)frame {
47 #pragma mark Accessors
49 - (UIScrollView*)scrollView {
51 _scrollView.reset([[UIScrollView alloc] initWithFrame:CGRectZero]);
53 return _scrollView.get();
62 - (void)layoutSubviews {
63 [super layoutSubviews];
65 // Early return if the bounds' size hasn't changed since the last layout.
66 if (CGSizeEqualToSize(_lastLayoutSize, self.bounds.size))
68 _lastLayoutSize = self.bounds.size;
71 self.scrollView.frame = self.bounds;
75 UIEdgeInsetsInsetRect(self.bounds, self.scrollView.contentInset);
76 CGSize viewSize = [self.view sizeThatFits:contentRect.size];
77 self.view.frame = CGRectMake(0.0, 0.0, viewSize.width, viewSize.height);
79 // UIScrollViews only scroll vertically if the content size's height is
80 // greater than that of its content rect.
81 if (viewSize.height <= CGRectGetHeight(contentRect)) {
82 CGFloat singlePixel = 1.0f / [[UIScreen mainScreen] scale];
83 viewSize.height = CGRectGetHeight(contentRect) + singlePixel;
85 self.scrollView.contentSize = viewSize;