WKWebview & UIImageView 在 UIScrollView 内以编程方式自动布局 Objective-C

Posted

技术标签:

【中文标题】WKWebview & UIImageView 在 UIScrollView 内以编程方式自动布局 Objective-C【英文标题】:WKWebview & UIImageView Inside UIScrollView programmatically autolayout Objective-C 【发布时间】:2018-01-17 08:40:32 【问题描述】:

我想以编程方式在 UIScrollView 中实现 WKWebView 和 UIImageView,但它不能满足我的要求。我首先将 UIImageView 和 WKWebView 添加到 scrollView,然后将 scrollView 添加到 ViewController 子视图。我为它编写了如下的Objective-C代码。

#import "StoryDetailViewController.h"
#import <WebKit/WebKit.h>
@interface StoryDetailViewController ()
//@property(strong,nonatomic) WKWebView *webView;
@end

@implementation StoryDetailViewController
    UIScrollView* scrollView;
    WKWebView* webView;
    UILabel* title;
    UIImageView* imageView;


- (void)viewDidLoad 
    [super viewDidLoad];

    [self setUpSubview];

    // Do any additional setup after loading the view.


-(void)setUpSubview

    scrollView = [[UIScrollView alloc]init];
    scrollView.backgroundColor=[UIColor redColor];
    scrollView.translatesAutoresizingMaskIntoConstraints=NO;
    imageView=[[UIImageView alloc]init];
    imageView.backgroundColor=[UIColor yellowColor];
    imageView.translatesAutoresizingMaskIntoConstraints=NO;
    webView = [[WKWebView alloc]init];
    webView.backgroundColor=[UIColor purpleColor];
    webView.translatesAutoresizingMaskIntoConstraints=NO;
    [scrollView addSubview:imageView];
    [scrollView addSubview:webView];
    [self.view addSubview:scrollView];

    [self addConstraints];

-(void)addConstraints

    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                          attribute:NSLayoutAttributeWidth
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:scrollView
                                                          attribute:NSLayoutAttributeWidth
                                                         multiplier:1.0
                                                           constant:0.0]];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                          attribute:NSLayoutAttributeHeight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:scrollView
                                                          attribute:NSLayoutAttributeHeight
                                                         multiplier:0.35
                                                           constant:0.0]];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                          attribute:NSLayoutAttributeLeft
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:scrollView
                                                          attribute:NSLayoutAttributeLeft
                                                         multiplier:1.0
                                                           constant:0.0]];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:scrollView
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:0.0]];

    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:webView
                                                           attribute:NSLayoutAttributeWidth
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:scrollView
                                                           attribute:NSLayoutAttributeWidth
                                                          multiplier:1.0
                                                            constant:0.0]];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:webView
                                                           attribute:NSLayoutAttributeLeft
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:scrollView
                                                           attribute:NSLayoutAttributeLeft
                                                          multiplier:1.0
                                                            constant:0.0]];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:webView
                                                           attribute:NSLayoutAttributeTop
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:imageView
                                                           attribute:NSLayoutAttributeBottom
                                                          multiplier:1.0
                                                            constant:10.0]];
    [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:webView
                                                           attribute:NSLayoutAttributeBottom
                                                           relatedBy:NSLayoutRelationEqual
                                                              toItem:scrollView
                                                           attribute:NSLayoutAttributeBottom
                                                          multiplier:1.0
                                                            constant:0.0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
                                                          attribute:NSLayoutAttributeLeft
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeLeft
                                                         multiplier:1.0
                                                           constant:0.0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
                                                          attribute:NSLayoutAttributeRight
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeRight
                                                         multiplier:1.0
                                                           constant:0.0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
                                                          attribute:NSLayoutAttributeBottom
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeBottom
                                                         multiplier:1.0
                                                           constant:0.0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:scrollView
                                                          attribute:NSLayoutAttributeTop
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeTop
                                                         multiplier:1.0
                                                           constant:0.0]];


@end

**我正在使用 WKwebview,因为 UIWebView 在 Xcode 9 中已弃用,而情节提要中的 WKwebview 仅支持 ios 11 ** 所以如果代码错误,请帮忙。

【问题讨论】:

【参考方案1】:

首先你要把这段代码放在 viewDidLayoutSubviews

-(void)viewDidLayoutSubviews 

 [self setUpSubview];


其次,您应该在滚动视图中创建一个内容视图并向其中添加元素,但作为一种解决方法,添加这两个约束

  [scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                      attribute:NSLayoutAttributeRight
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:scrollView
                                                      attribute:NSLayoutAttributeRight
                                                     multiplier:1.0
                                                       constant:0.0]];

  [self.view addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                  attribute:NSLayoutAttributeWidth
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:self.view
                                                  attribute:NSLayoutAttributeWidth
                                                 multiplier:1.0
                                                   constant:0.0]];

并删除它,因为滚动视图期望它的大小来自子元素,反之亦然

[scrollView addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                      attribute:NSLayoutAttributeWidth
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:scrollView
                                                      attribute:NSLayoutAttributeWidth
                                                     multiplier:1.0
                                                       constant:0.0]];

第三个给imageView的高度不等于滚动视图删除它并给一个静态高度或与self.view成比例的高度

  [imageView addConstraint:[NSLayoutConstraint constraintWithItem:imageView
                                                  attribute:NSLayoutAttributeHeight
                                                  relatedBy:NSLayoutRelationEqual
                                                     toItem:nil
                                                  attribute:NSLayoutAttributeNone
                                                 multiplier:1.0
                                                   constant:200.0]];

最后用 webview 做 setp 2,3

【讨论】:

以上是关于WKWebview & UIImageView 在 UIScrollView 内以编程方式自动布局 Objective-C的主要内容,如果未能解决你的问题,请参考以下文章

iOS WKWebView 加载进度条导航栏返回&关闭 (Swift 4)

为啥我在 iOS 9 上的 WKWebView 中的音频流无法播放?

WKWebView 实例可以在加载时显示网页吗?

WkWebview 被 Angular 冻结了

自动将 HTML <img> 从 WKWebView 保存到应用程序的 tmp 目录

在 UIWebView 和 WkWebview 中禁用 javascript 警报?