UIScrollView(Auto Layout)

UIScrollView(UIScrollViewDelegate)

UIScrollView(Auto Layout)


在工作中使用UIScrollView的时候我们通常会做适配,也就是Auto Layout。这里我会使用纯代码和Storyboard界面做适配。

适配的核心就是约束,对UIScrollView做适配时要记住,对外的约束是显示区域,对内的约束是滑动区域。我总结为八个字:对外显示对内滑动。

1 纯代码适配

//
//  YJAutoLayoutSVVC.swift
//  UI
//
//  CSDN:http://blog.csdn.net/y550918116j
//  GitHub:https://github.com/937447974/Blog
//
//  Created by yangjun on 15/12/17.
//  Copyright © 2015年 阳君. All rights reserved.
//

import UIKit

class YJAutoLayoutSVVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "纯代码AutoLayout"
        // 创建UIScrollView和UIImageView
        let scrollView  = UIScrollView()
        let imageView = UIImageView()
        imageView.image = UIImage(named: "ScrollViewBigImage")

        // 添加视图
        self.view.addSubview(scrollView)
        scrollView.addSubview(imageView)

        // 开启AutoLayout
        scrollView.translatesAutoresizingMaskIntoConstraints  = false;
        imageView.translatesAutoresizingMaskIntoConstraints = false;

        // self.view和ScrollView的约束
        // 显示区域,方式1添加约束
        let centerXC = NSLayoutConstraint(item: scrollView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0) // 中心x点对齐
        let centerYC = NSLayoutConstraint(item: scrollView, attribute: .CenterY, relatedBy: .Equal, toItem: self.view, attribute: .CenterY, multiplier: 1, constant: 0) // 中心y点对齐
        self.view.addConstraints([centerXC, centerYC]) // 组添加
        let heightC = NSLayoutConstraint(item: scrollView, attribute: .Height, relatedBy: .Equal, toItem: self.view, attribute: .Height, multiplier: 1, constant: 0) // 等高
        self.view.addConstraint(heightC) // 单一添加
        NSLayoutConstraint(item: scrollView, attribute: .Width, relatedBy: .Equal, toItem: self.view, attribute: .Width, multiplier: 1, constant: 0).active = true // 等宽,立即执行

        // 滑动区域,方式2添加约束
        // NSLayoutYAxisAnchor封装NSLayoutConstraint写法
        scrollView.topAnchor.constraintEqualToAnchor(imageView.topAnchor).active = true // Top
        scrollView.bottomAnchor.constraintEqualToAnchor(imageView.bottomAnchor).active = true // Bottom
        scrollView.leadingAnchor.constraintEqualToAnchor(imageView.leadingAnchor).active = true // leading
        scrollView.trailingAnchor.constraintEqualToAnchor(imageView.trailingAnchor).active = true // trailing
    }

}

运行效果图

这里为大家展示了三种约束效果。

2 Storyboard约束

Storyboard约束讲解起来比较麻烦,就用一个视屏为大家演示。

 


其他

源代码

Swift

参考资料

UIScrollView Class Reference

UIScrollView And Autolayout

Auto Layout Guide

文档修改记录

时间描述
2015-12-18博文完成

版权所有

CSDN:http://blog.csdn.net/y550918116j

GitHub:https://github.com/937447974/Blog

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值