最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】swift如何实现不传入frame而实现自动布局

Swift crifan 2908浏览 0评论

想要实现一个自定义的UIView,写其init函数时发现,比如要设置对应的frame才行,否则没发初始化:

import UIKit

class FileBubbleView: UIView {
    var iconImageView:UIImageView
    var nameLabel:UILabel
    var sizeLabel:UILabel
   
    init(message:Message){
        self.iconImageView = UIImageView()
        self.nameLabel = UILabel()
        self.sizeLabel = UILabel()
       
        super.init(frame: )
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

如果写成:

    init(frame:CGRect, message:Message){
        self.iconImageView = UIImageView()
        self.nameLabel = UILabel()
        self.sizeLabel = UILabel()
       
        super.init(frame: frame)
    }

结果调用时必须传递frame:

                self.fileBubbleView = FileBubbleView(frame: CGRectZero, message: message)

但是此处传入的frame,就限定了宽度和高度啊。。。

就不是我所希望的auto layout了。。。

此处想要实现:

一个支持自动布局的UIView,所以不能,不想,去设置对应的frame。

swift autolayout programmatically not set frame

swift UIView autolayout not set frame

ios – Why can’t I change the view’s frame size in Swift? – Stack Overflow

ios – Change frame programmatically with auto layout – Stack Overflow

ios – swift change height of UIView – Stack Overflow

Beginning Auto Layout Tutorial in iOS 7: Part 2 – Ray Wenderlich

change UIView Size -height SWIFT – Stack Overflow

swift UIView autolayout init without frame

swift subclass UIView autolayout

ios – Proper practice for subclassing UIView? – Stack Overflow

ios – Swift: Custom UIView not resizing to constraints – Stack Overflow

Auto layout best practices for minimum pain — Medium

Auto layout and view initializations – The.Swift.Dev.

Working With Auto Layout on iOS · The Main Thread

Creating views by subclassing UIView – Swift Essentials [Book]

Simple Auto Layout in Swift · The Main Thread

About Those Missing Views | Introducing iOS Auto Layout | InformIT

swift   UIView init without frame

swift   UIView init without frame programmatically

swift   UIView init no frame programmatically

ios – How to implement two inits with same content without code duplication in Swift? – Stack Overflow

Principles on iOS Custom View Implementation in Swift And Best Practice

Initialize Swift subclass of UIView, designed in .xib · GitHub

swift   UIView init without frame

swift init autolayout UIView

AFViewHelper/AFViewAutoLayout.swift at master · melvitax/AFViewHelper · GitHub

好像,对于使用了:

自动布局的情况下,初始化frame,可以直接设置为CGRectZero的???

之后的自动布局,是可以自动生效的??

Doing it all in code – roopc.net

swift CGRectZero autolayout UIView

ios – Use autolayout to set dynamic UIView to match container view – Stack Overflow

ios – Using auto layout constraints programmatically create four UIView all with equal height and width – Stack Overflow

[总结]

总体逻辑是:

1.初始化UIView传入的frame值是CGRectZero

2.然后设置:setTranslatesAutoresizingMaskIntoConstraints为false

3.添加constraint

此处,我用了Cartography实现自动布局

-》其会自动帮忙做2和3

-》所以只需要按照Cartography的语法设置自动布局的约束即可。

-》然后实现自定义的UIView的时候,使用:

import UIKit

class FileBubbleView: UIView {
    //xxx
   
    init(frame:CGRect, message:Message){
        //init xxx
       
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

调用时传入CGRectZero:

self.fileBubbleView = FileBubbleView(frame: CGRectZero, message: message)

即可。

此种做法,待后续验证是否能够正常的实现自动布局。

转载请注明:在路上 » 【记录】swift如何实现不传入frame而实现自动布局

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.344 seconds, using 22.28MB memory