★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-yrmbbzjr-ma.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
目录:[Swift]通天遁地Swiftios
本文将演示如何给视图添加锚点约束。git
锚点默认位于视图中心点的位置。github
首先确保在项目中已经安装了所需的第三方库。swift
点击【Podfile】,查看安装配置文件。微信
1 platform :ios, ‘12.0’ 2 use_frameworks! 3 4 target 'DemoApp' do 5 source 'https://github.com/CocoaPods/Specs.git' 6 pod 'Neon' 7 end
根据配置文件中的相关配置,安装第三方库。ide
而后点击打开【DemoApp.xcworkspace】项目文件。布局
在项目导航区,打开视图控制器的代码文件【ViewController.swift】post
如今开始编写代码,建立一个滚动视图,并在滚动视图中添加一个标签控件。spa
1 import UIKit 2 //在当前的类文件中,引入已经安装的第三方类库 3 import Neon 4 5 class ViewController: UIViewController { 6 7 //添加一个视图属性 8 var neonView : UIView! 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 // Do any additional setup after loading the view, typically from a nib. 13 14 //对视图进行初始化 15 neonView = UIView() 16 //设置视图的背景颜色为橙色 17 neonView.backgroundColor = UIColor.orange 18 //将视图添加到根视图中 19 self.view.addSubview(neonView!) 20 21 //须要放在addSubView方法的后面 22 //经过视图对象的扩展方法,将视图的锚点, 23 //固定在父视图的中心位置, 24 //并设置视图对象的宽度和高度都是100 25 neonView.anchorInCenter(width: 100, height: 100) 26 } 27 28 //添加一个锚点,用来响应子视图的布局, 29 //是即将发生变化时的事件。 30 override func viewWillLayoutSubviews() { 31 //当布局即将发生变化时, 32 //从新设置视图的约束关系。 33 neonView.anchorInCenter(width: 100, height: 100) 34 } 35 36 override func didReceiveMemoryWarning() { 37 super.didReceiveMemoryWarning() 38 // Dispose of any resources that can be recreated. 39 } 40 }
改变模拟器的方向:
【Hardware】->【Rotate Left】
当模拟器的方向发生变化时,
橙色视图仍然保持原来的尺寸。
而且始终位于模拟器的中心位置。