IOS学习笔记- UI

151 阅读2分钟

常用UI组件

to-do

布局

Auto Layout

个人感觉这个设计有点反人类!!!

比如给一个view添加约束:

截屏2022-10-26 14.27.40.png
      let widthConstraint = NSLayoutConstraint(item: redView, attribute: .width, relatedBy: .equal, toItem: **nil**, attribute: .notAnAttribute, multiplier: 0, constant: 400)

        //高度约束

       let heightConstraint = NSLayoutConstraint(item: redView, attribute: .height, relatedBy: .equal, toItem: **nil**, attribute: .notAnAttribute, multiplier: 0, constant: 150)

        //顶部约束

        let topConstraint = NSLayoutConstraint(item: redView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1.0, constant: 100)

        //左侧约束

        let leftConstraint = NSLayoutConstraint(item: redView, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1.0, constant: 100)

        view.addConstraints([widthConstraint,heightConstraint,leftConstraint,topConstraint])

SnapKit

安装cocoapods

参考教程,教程有点小问题,遇到报错搜索即可。

通过cocoapods安装SnapKit

终端中进入项目,pod init会生成一个podfile文件,打开修改:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'SnapKit', '~> 5.6.0'
end

这样输入pod install 会一直卡在下面:

Cloning spec repo 'cocoapods' from 'https://github.com/CocoaPods/Specs.git'

换源其实也会卡在换源的过程中

podfile修改成下面内容

platform :ios, '10.0'
target 'Test' do
    pod 'SnapKit', '~> 5.6.0'
end

复制终端代理命令,写到终端,再pod install

配置成功后,文件夹如下,多了一个xcworkspace,podfile.lock文件:

截屏2022-10-26 16.47.09.png

双击xcworkspace文件进入项目。

使用SnapKit

参考教程:

www.jianshu.com/p/2bad53a2a…

一个计算器页面的案例:

www.hangge.com/blog/cache/…

一个登录页面的案例:

www.hangge.com/blog/cache/…

  • x轴在上,y轴在左,原点在左上角
  • offset偏移是按照父视图在坐标轴投影方向,顺着坐标轴方向就是+,逆着坐标轴方向-
  • 设定布局约束前必须加入父视图,否则报错无法约束。

TabBarController

配置一下项目
  1. 教程1
  2. 教程2

主流做法是,先删main.storyboard,再删info里的配置,注释掉带scene的方法

在我的电脑xcode.14 里,删掉main.storyboard,会报错找不到main;把main.storyboard配置回来,会黑屏,然后把info文件里的配置删除掉,这时运行,但是手打 UIScene 还是找不到 main?