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

[已解决]swift如何继承UIView设置frame

Swift crifan 4000浏览 0评论
之前在UIView的子类中,init中设置的了frame,但是都不起效果:
x和y的位置,都是乱的。
只有去
override func layoutSubviews() {
才可以再去设置对应的x和y值,设置位置。
swift uiview subclass init
swift uiview subclass example
参考:
xcode – Extended UIView class using Swift – Stack Overflow
cocoa touch – How do I write a custom init for a UIView subclass in Swift? – Stack Overflow
ios – Swift subclass UIView – Stack Overflow
搜:
swift uiview subclass frame not working
swift uiview subview frame
最后是:
import UIKit

class DemoUiviewSubclass : UIView {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override init(frame: CGRect) {
        print("DemoUiviewSubclass init()")
        print("Input frame=\(frame)")

        super.init(frame: frame)

        self.backgroundColor = UIColor.purpleColor()


        let tableView = UITableView(frame: CGRectMake(
            0, // !!! Note: this x is relative to current sub view
            0, // !!! Note: this y is relative to current sub view
            100,
            40)
        )

        self.addSubview(tableView)
    }
}

即可。

转载请注明:在路上 » [已解决]swift如何继承UIView设置frame

发表我的评论
取消评论

表情

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

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