美文网首页
DeclareLayoutSwift实例1——钉钉消息界面

DeclareLayoutSwift实例1——钉钉消息界面

作者: huangzhouhong | 来源:发表于2018-04-07 13:12 被阅读0次

目标

使用DeclareLayoutSwift来实现界面,代码非常简短,完全手写代码,不使用Storyboard和xib,也不使用约束。
要实现的效果:

要实现的效果

1.导航栏

nav.jpg

1.1 导航栏结构分析

钉钉-消息.png
    func createTitleView(title:String){
        self.navigationController?.navigationBar.barTintColor = .white
        let titleView = HostView {
            Grid(.columns <- [.star(1),.auto]){ // ①
                [Label(.text <- title, .fontSize <- 20),//②
                 StackPanel(.gridColumnIndex <- 1,.orientation <- .Horizontal) {//③
                    [Button(.image <- #imageLiteral(resourceName: "Msg_nav1")),//④
                     Button(.image <- #imageLiteral(resourceName: "Msg_nav2"), .margin <- Insets(vertical: 0, horizontal: 20)),
                     Button(.image <- #imageLiteral(resourceName: "Msg_nav3"))]
                    }]
            }
        }
        self.navigationItem.titleView = titleView
    }

  • 使用Grid,将导航栏分成2列
  • 第2列为自动(.auto,根据内容设置宽度),用来放置右侧图标。因为这一列是根据内容自动适应宽度的,增加图标或减少图标时不需要修改
  • 第1列占据剩下宽度。.star(1)的表示这一列是按比例分配。如:[.star(1),.star(2)]表示按1:2分配空间。这里没有多个列用.star表示,所以第1列会占用所有剩下空间。

  • 声明一个Label,并设置文本和字体大小
  • Label是Grid的子元素
  • 由于位于Grid中,可以设置其所在的行或列,不设置默认不第1行第1列

  • StackPanel表示一个水平或竖直方向的布局,这里设置为水平方向.orientation <- .Horizontal,表示把子元素从左到右水平排列。
  • 将StackPanel放到Grid的第2列(.gridColumnIndex <- 1)

  • 创建Button,并设置Button的图片
  • 也可以写为Button(.image <- "Msg_nav1")

2. 创建UITableView Cell

cell.jpg

2.1 Cell结构分析

Cell结构
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let element = Grid(.columns <- [.auto, .star(1)], .padding <- Insets(vertical: 8, horizontal: 20)) {
            [Image(.width <- 50, .image <- #imageLiteral(resourceName: "Msg_List"), .margin <- Insets(right: 10)),
             StackPanel(.gridColumnIndex <- 1) {
                 [Grid(.columns <- [.star(1), .auto]) {
                     [Label(.text <- "钉钉运动", .fontSize <- 17),
                      Label(.gridColumnIndex <- 1, .text <- "下午 8:36", .fontSize <- 13, .textColor <- UIColor(white: 0.8, alpha: 1))]
                 },
                  Label(.text <- "IT技术中心 获得3月24日 XXX有限公司全员步数第一", .fontSize <- 15, .textColor <- .gray, .margin <- Insets(top: 8))]
            }]
        }
        return tableView.makeCell(element: element)
    }

paddingmargin分别定义内边距和外边距类型为Insets(UIEdgeInsets)

完整代码

import DeclareLayoutSwift
import UIKit

class MessageVC: UIViewController, UITableViewDelegate, UITableViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.createTitleView(title: "钉钉")

        self.view.hostElement {
            Table(.delegate <- self, .dataSource <- self)
        }
    }

    func createTitleView(title: String) {
        self.navigationController?.navigationBar.barTintColor = .white
        let titleView = HostView {
            Grid(.columns <- [.star(1), .auto]) {
                [Label(.text <- title, .fontSize <- 20),
                 StackPanel(.gridColumnIndex <- 1, .orientation <- .Horizontal) {
                     [Button(.image <- #imageLiteral(resourceName: "Msg_nav1")),
                      Button(.image <- #imageLiteral(resourceName: "Msg_nav2"), .margin <- Insets(vertical: 0, horizontal: 20)),
                      Button(.image <- #imageLiteral(resourceName: "Msg_nav3"))]
                }]
            }
        }
        self.navigationItem.titleView = titleView
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 100
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let element = Grid(.columns <- [.auto, .star(1)], .padding <- Insets(vertical: 8, horizontal: 20)) {
            [Image(.width <- 50, .image <- #imageLiteral(resourceName: "Msg_List"), .margin <- Insets(right: 10)),
             StackPanel(.gridColumnIndex <- 1) {
                 [Grid(.columns <- [.star(1), .auto]) {
                     [Label(.text <- "钉钉运动", .fontSize <- 17),
                      Label(.gridColumnIndex <- 1, .text <- "下午 8:36", .fontSize <- 13, .textColor <- UIColor(white: 0.8, alpha: 1))]
                 },
                  Label(.text <- "IT技术中心 获得3月24日 XXX有限公司全员步数第一", .fontSize <- 15, .textColor <- .gray, .margin <- Insets(top: 8))]
            }]
        }
        return tableView.makeCell(element: element)
    }
}

Demo已经放在https://github.com/huangzhouhong/DeclareLayoutSwift

相关文章

  • DeclareLayoutSwift实例1——钉钉消息界面

    目标 使用DeclareLayoutSwift来实现界面,代码非常简短,完全手写代码,不使用Storyboard和...

  • 钉钉家校本--家长交作业流程

    1. 进入提交作业的界面 有以下两种方式进入: 方式一 直接提交 点击钉钉群消息界面中老师的发布作业信息,直接进...

  • 钉钉消息

    一、自定义机器人 郑重警告⚠️ 每个机器人每分钟只可以发送 20 条信息,多了,会被禁止使用 10 分钟. she...

  • 误会了钉钉考勤打卡

    早期我写了一篇“钉钉的考勤打卡这样真的好吗?”,主要阐述员工进入钉钉界面,默认页是消息页面,需要切换页面,点击标签...

  • 钉钉消息推送

    首先,我们来看一下钉钉消息推送的官方sdkhttps://open-doc.dingtalk.com/microa...

  • <转发>VB.NET应用钉钉开发接口(二)

    1、前言 钉钉开发基本都是java做应用实例,缺乏.net开发应用实例,尤其是缺乏http://VB.NET。 根...

  • python实现钉钉群消息推送

    Python实现钉钉群消息推送 对于某些自动化任务执行结果反馈,使用钉钉机器人消息替换繁琐的邮件发送方式使用钉钉机...

  • python钉钉机器人发消息

    用于钉钉群,定时发消息之类的 1、创建钉钉机器人 2、创建机器人 3、创建成功,拿到webhook 4、创建一个钉...

  • 钉钉审批应用实例

    用印申请/差旅费报销/采购申请/备用金申请/付款申请 用印申请 -编辑 -审批人设置 差旅费报销 -编辑 -审批人...

  • 17 grafana alert 一篇就够

    17.1 grafana 配置 17.2 钉钉消息

网友评论

      本文标题:DeclareLayoutSwift实例1——钉钉消息界面

      本文链接:https://www.haomeiwen.com/subject/ocbkcftx.html