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 // Backing objectect for |self.scrollView|.
12 base::scoped_nsobject<UIScrollView> _scrollView;
13 // Backing object for |self.view|.
14 base::scoped_nsobject<UIView> _view;
19 @implementation CRWGenericContentView
21 - (instancetype)initWithView:(UIView*)view {
22 self = [super initWithFrame:CGRectZero];
25 _view.reset([view retain]);
26 _scrollView.reset([[UIScrollView alloc] initWithFrame:CGRectZero]);
27 [self addSubview:_scrollView];
28 [_scrollView addSubview:_view];
29 [_scrollView setBackgroundColor:[_view backgroundColor]];
34 - (instancetype)initWithCoder:(NSCoder*)decoder {
39 - (instancetype)initWithFrame:(CGRect)frame {
44 #pragma mark Accessors
46 - (UIScrollView*)scrollView {
48 _scrollView.reset([[UIScrollView alloc] initWithFrame:CGRectZero]);
50 return _scrollView.get();
59 - (void)layoutSubviews {
60 [super layoutSubviews];
63 self.scrollView.frame = self.bounds;
67 UIEdgeInsetsInsetRect(self.bounds, self.scrollView.contentInset);
68 CGSize viewSize = [self.view sizeThatFits:contentRect.size];
69 self.view.frame = CGRectMake(0.0, 0.0, viewSize.width, viewSize.height);
71 // UIScrollViews only scroll vertically if the content size's height is
72 // creater than that of its content rect.
73 if (viewSize.height <= CGRectGetHeight(contentRect)) {
74 CGFloat singlePixel = 1.0f / [[UIScreen mainScreen] scale];
75 viewSize.height = CGRectGetHeight(contentRect) + singlePixel;
77 self.scrollView.contentSize = viewSize;