【问题标题】:WKWebview & UIImageView Inside UIScrollView programmatically autolayout Objective-CWKWebview & UIImageView 在 UIScrollView 内以编程方式自动布局 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 ** 所以如果代码错误,请帮忙。

【问题讨论】:

    标签: ios objective-c autolayout wkwebview


    【解决方案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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-06
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多