项目中不常出现但非常有用的设置

没有更新Blog已经很久了,实在是懒的惭愧😂。终于在经历了去年的风波和今年的疫情后安稳了下来,也因为Swift也只是在之前工作之余中学过一点,并没有在真正的项目中使用过,所以还是很有必要好好的记录记录。

从手中项目中的表现来看主要功能的实现还是没什么问题的,主要的问题在于一些比较特殊情况的实现或者说应该注意的点。这些情况实现虽然出现的不是那么的频繁,但能在不查资料的情况下使用它们还是再好不过的。以下都是在项目中碰到的情况。

Swift 4.0

自定义TabelViewCell 的 arrowImage 在 .scaleAspectFit 状态下如何修改原生的箭头图标

1
2
3
4
5
6
let arrowImage = UIImageView.init(frame: CGRect(x: 0, y: 0, width: 20, height: 20))

arrowImage.contentMode = .scaleAspectFit

arrowImage.image = UIImage(named: "next_icon")
cell.accessoryView = arrowImage

虽然可以在自定义的Cell中设置统一的箭头Icon,但在Cell的布局相对简单又觉得创建一个自定义的Cell又显麻烦,而且用自带的Cell样式就能搞定的情况下要修改箭头的样式。使用这种方式就很方便了。

隐藏TabelViewCell 没有内容的区间(不显示没有内容区域的分割线)

1
tableView.tableFooterView = UIView()

统一设置Navigation样式

因为项目UI的样式其Navigation都是统一的——带有颜色的背景、导航标题颜色为白色且字号固定

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 直接在AppDelegate中设置
// 设置背景图
var backGroundImage = UIImage(named: "nav_bg")
backGroundImage = backGroundImage?.resizableImage(withCapInsets: UIEdgeInsets.zero, resizingMode: UIImageResizingMode.stretch)
let navBar = UINavigationBar.appearance()
navBar.setBackgroundImage(backGroundImage, for: .default)

// 设置title字体和颜色
navBar.tintColor = .white
navBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white,NSAttributedString.Key.font : UIFont.systemFont(ofSize: 20, weight: .bold)]

// 隐藏Navigation返回按钮文字
let navBarItem = UIBarButtonItem.appearance()
navBarItem.setBackButtonTitlePositionAdjustment(UIOffset(horizontal: -100, vertical: 0), for: .default)

如果要单独隐藏摸个页面的Navigation返回按钮文字 可以使用下面方法

1
2
3
4
// push时设置
let item = UIBarButtonItem(title: "", style: .plain, target: self, action: nil)
self.navigationItem.backBarButtonItem = item;
self.navigationController?.pushViewController(ViewController(), animated: true)

指定部分字符串颜色和字体

这个碰到的情况还是很高的,之前在项目中还傻傻的用两个Label来实现 ( ̄▽ ̄///)

1
2
3
4
5
6
7
8
9
let string = NSString(string: (terms.titleLabel?.text)!)

// range很好的定位了要设置的部分
let range = string.range(of: "服务协议和隐私条款")

let mutableArr = NSMutableAttributedString.init(string: (terms.titleLabel?.text)!)
mutableArr.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.black, range: range)

terms.titleLabel?.attributedText = mutableArr

修改placeholder的字体属性

1
2
3
textField.placeholder = placeholderArray[indexPath.row]

textField.attributedPlaceholder = NSAttributedString.init(string: placeholderArray[indexPath.row], attributes: [NSAttributedString.Key.foregroundColor : RGBAlpa(169,199,218,1)])

同时带标题和图片的按钮

1
2
3
4
5
6
7
8
9
10
11
12
let button = UIButton()
button.setTitle("5000积分", for: .normal)
button.setImage(UIImage(named: "commodity_jifen_icon2"), for: .normal)
button.imageEdgeInsets = UIEdgeInsets(top: -30, left: 0, bottom: 0, right: 0)
button.titleEdgeInsets = UIEdgeInsets(top: 30, left: -30, bottom: 0, right: 0)
button.backgroundColor = .red
cell.addSubview(button)
button.snp_makeConstraints { (ConstraintMaker) in
ConstraintMaker.centerX.equalToSuperview()
ConstraintMaker.size.equalTo(CGSize(width: WIDTH / 3, height: 88))
ConstraintMaker.bottom.equalToSuperview().offset(-10)
}

主要还是用到了UIButton的imageEdgeInsets和titleEdgeInsets 两个属性来控制图片和标题在button中的位置。图片和标题默认的Y轴位置都是根据按钮的横向水平位置。

图片的位置居左,标题的X轴的坐标则在图片的右侧,宽度则为标题内容的宽度。

要想调整位置则根据当前的坐标通过UIEdgeInsets来调整。

UIEdgeInsets有 top、left、bottom、right 四个方向的属性 对应的属性值会在当前对应的坐标上进行加减 从而实现想要的位置。