iOS 项目开发问题记录-代码部分

前言

在今年七夕晚上终于上线了自己开发的第一款iOS App:《夜有所梦》。现在发布了第二个版本1.1.0,1.5.0正在开发中,也快更新了。

开发期间遇到了很多问题,都是作为新手需要走的坑,有编译器方面的、代码部分,还有打包上架部分的,这里记录的是代码方面遇到的问题。由于网上的资料不多,且有些都是比较老的,现在这个项目是基于swift5,iOS14,看能不能帮到遇到同样问题的小伙伴。

问题

title怎么设置

核心是VC包一层 UINavigationController,处理后就有导航栏

导航栏问题:UINavigationController可以用来给vc或者是UITabBarController加标题栏(navigationItem)。

启动时需要指定vc,vc需要用UINavigationController包裹,因为需要标题栏;如果是UITabBarController则不用包裹,因为UITabBarController里面用的是vc,在UITabBarController类里加vc时包裹就好,这样每个vc就有自己单独的navigationItem。

键盘监听

键盘监听是全局一个,注册第二个会调用第一个的监听

禁止 present的vc下拉返回

禁止 present的vc下拉返回,并监听下拉事件 :

https://www.jianshu.com/p/b18051f429d3

realm 使用

1、 如果更改数据库字段。需要修改DatabaseUtil里的dbVersion

2、打开数据库本地文件,在访达中点击右键,粘贴路径。查看数据库,下载dmg : https://studio-releases.realm.io/latest/download/mac-dmg

真机的话:xcode:window->Devices and Simulators ;点击省略号下载,然后显示包内容,再在doucument里打开文件。

https://blog.csdn.net/boildoctor/article/details/111983250

3、创建能排序的List

https://juejin.cn/post/7212911193200017464

4、排序

sorted() 裡的 ascending 接受的型別是 Bool , true 是正排序,false時為逆排序

//取出資料並將年齡由小排到大
let results = realm.objects(table.self).sorted(byKeyPath: "age")

//取出資料並將年齡由小大排到小
let results = realm.objects(RLM_User.self).sorted(byKeyPath: "age", ascending: false)

5、 realm 开线程取数据

子线程查询的数据,无法在主线程使用:

https://www.jianshu.com/p/b4b05c30201f

UILable 、UITextField、UITextView 使用场景

Text Programming Guide for iOS 上面提示到:

  • UILabel定义标签,显示静态文本字符串
  • UITextField定义一个文本字段,显示一行可编辑文本。
  • UITextView定义文本视图,显示多行可编辑文本。

虽然这些类实际上可以支持任意数量的文本的显示,但标签和文本字段旨在用于相对少量的文本,通常是单行。另一方面,文本视图旨在显示大量文本。 从 UITextView 类创建的文本视图对象,显示格式为段落,列和页面的文本,具有精细排版的所有特征,例如字距调整,连字,复杂的换行和对齐

所以如果想显示多行大量文本,建议使用 UITextView,能够获得更好的排版布局

UILable设置字/行间距

// 时间:字间距
// timeLabel.text = TimeUtil.getMDday(timeStamp: model.creatTime)
timeLabel.attributedText = NSAttributedString(string: TimeUtil.getMDday(timeStamp: model.creatTime),attributes: [NSAttributedString.Key.kern: 1])

// 内容:通过富文本来设置行间距
let paraph = NSMutableParagraphStyle()
//将行间距设置为28
paraph.lineSpacing = 5
//样式属性集合
var title = ""
if (model.title != nil) {
    title = model.title!.trimmingCharacters(in: .whitespacesAndNewlines)
}
let attributes = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 15),
                  NSAttributedString.Key.paragraphStyle: paraph]
titleLabel.attributedText = NSAttributedString(string: (title), attributes: attributes)
// 需要代码重新设置一下
titleLabel.lineBreakMode = .byTruncatingTail
titleLabel.sizeToFit()

去掉方法内的字段提示Key

方法命名时,前面加 “_

例如:**public** **class** **func** deleteTag(_ tag : Tag)

textField长按出现select,显示中文

解决办法:

1.首先保证手机系统是简体中文: 设置->通用->语言与地区

2.项目的info.plist文件中,增加 sorcecode是这样的,Localization那里

UITextView 加placeHolder

发梦境的页面已实现

https://stackoverflow.com/questions/27652227/add-placeholder-text-inside-uitextview-in-swift

点击UIButton调起emoji键盘

class EmojiButton: UIButton, UIKeyInput {

    var hasText: Bool = true

    // 需要加这个不然点击没反应
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        if !isFirstResponder {
            _ = becomeFirstResponder()
        }
    }
    
    override var textInputContextIdentifier: String? { "" } // return non-nil to show the Emoji keyboard ¯\_(ツ)_/¯

    func insertText(_ text: String) {
        setTitle(text, for: .normal) 
    }
    
    func deleteBackward() {}

    override var canBecomeFirstResponder: Bool { return true }

    override var canResignFirstResponder: Bool { return true }

    override var textInputMode: UITextInputMode? {
        for mode in UITextInputMode.activeInputModes {
            if mode.primaryLanguage == "emoji" {
                return mode
            }
        }
        return nil
    }
}

同一行的布局,自适应宽度

//设置lableIcon的宽度优先自适应,关键代码!!!
// setContentHuggingPriority是抗拉伸属性,属性低更容易被拉伸,被拉伸是指控件的宽度超过文字宽度,用空白填充
// setContentCompressionResistancePriority是抗压缩属性,属性低更容易被压缩,被压缩是指控件的宽度不足以放下文字,用省略号...代替
lableIcon.setContentHuggingPriority(UILayoutPriority(rawValue: 1000), for: .horizontal)
lableIcon.setContentCompressionResistancePriority(UILayoutPriority(rawValue: 1000), for: .horizontal)

iOS Swift setContentCompressionResistancePriority与setContentHuggingPriority

https://www.jianshu.com/p/d5ed8537cc79

UILabel超过3行后显示…

// 在初始化时设置可能不管用,需要代码重新设置一下
titleLabel.lineBreakMode = .byTruncatingTail

UIScrollView使用

1、根布局需要一个View,然后view里再加布局

uiScrollView.snp.makeConstraints { (make) in
    make.edges.equalTo(view)
}
contentView.snp.makeConstraints { (make) in
    make.top.bottom.equalTo(uiScrollView)
    make.left.right.equalTo(view) // 确定的宽度,因为垂直滚动
}

2、contentView再加2个view,然后2个view里加子view,注意第一个子view要设置top,最后一个子view要一定要设置bottom

UIScrollView简单使用:https://juejin.cn/post/6970515865003360263

SnapKit布局tip:

/**
*示例:
*label1.snp.makeConstraints { (make) in
*make.left.right.equalTo(contentView).inset(20)  内边距
*make.top.equalTo(contentView).offset(20) 外边距
*}
*确定scrollView的宽度
*ConstraintMaker.left.right.equalTo(view);
*确定scrollView显示的高度
*ConstraintMaker.bottom.equalTo(view)
*/

修改导航栏按钮文字粗体

// navigationItem 系统默认的字号大小是 16
uIBarButtonItem?.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16, weight: .medium)], for: .normal)

三目运算符

• 冒号前面无空格, 后面有一个空格, 三目运算符? : 除外和空字典 [:] 除外

多语言支持

Swift实现本地化多语言,咋操作? - 掘金

1.在info里加入语言

2.新建语言String文件

3.建2个相同的Key,分别写中文和英文,然后用方法替换

4.如果是应用名,需要新建另一个文件

结语

再次推荐一下开发的应用:《夜有所梦》

灵感来源:日有所思 · 夜有所梦。主要是记录平时的所思和梦境,欢迎体验。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值