最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]swift willMoveToSuperview的含义

Swift crifan 946浏览 0评论

折腾:

[已解决]swift实现自定义的UITextField

期间,要去搞清楚:

willMoveToSuperview

的含义和用法。

搜:

willMoveToSuperview

参考:

UIView如何管理它的子视图 – iOS开发笔记 – 博客频道 – CSDN.NET

“视图移动前会发出willMoveToSuperview:回调”

iOS中willMoveToSuperview:方法 – 花园晓雨 – 博客园

“当自己重写一个UIView的时候有可能用到这个方法,当本视图的父类视图改变的时候,系统会自动的执行这个方法.newSuperview是本视图的新父类视图.newSuperview有可能是nil.”

willMoveToSuperview – 简书

“willMoveToSuperview会被执行两次

When a view is added to a superview, the system sends willMoveToSuperview: to the view. The parameter is the new superview.When a view is removed from a superview, the system sends willMoveToSuperview: to the view. The parameter is nil.You can’t prevent the system from sending willMoveToSuperview: when you remove the view from its superview, but you can check the parameter:

– (void)willMoveToSuperview:(UIView *)newSuperview { if (newSuperview != nil) { // not a removeFromSuperview situation } }

UIView Class Reference

Layout:

sizeThatFits: – Implement this method if you want your view to have a different default size than it normally would during resizing operations. For example, you might use this method to prevent your view from shrinking to the point where subviews cannot be displayed correctly. 

layoutSubviews – Implement this method if you need more precise control over the layout of your subviews than either the constraint or autoresizing behaviors provide. 

didAddSubview:willRemoveSubview: – Implement these methods as needed to track the additions and removals of subviews.

willMoveToSuperview:didMoveToSuperview – Implement these methods as needed to track the movement of the current view in your view hierarchy.

willMoveToWindow:didMoveToWindow – Implement these methods as needed to track the movement of your view to a different window.

willMoveToSuperview(_:)

Tells the view that its superview is about to change to the specified superview.

Declaration

SWIFT

func willMoveToSuperview(_ newSuperview: UIView?)

Parameters

newSuperview

A view object that will be the new superview of the receiver. This object may be nil.

Discussion

The default implementation of this method does nothing. Subclasses can override it to perform additional actions whenever the superview changes.

Availability

Available in iOS 2.0 and later.

关于ios6 willMoveToSuperview函数的一些问题 – blog

willMoveToSuperview

下面是官方的说明:

Tells the view that its superview is about to change to the specified superview.

  • (void)willMoveToSuperview:(UIView *)newSuperview Parameters newSuperview A view object that will be the new superview of the receiver. This object may be nil. Discussion The default implementation of this method does nothing. Subclasses can override it to perform additional actions whenever the superview changes.

Availability

Available in iOS 2.0 and later.

[总结]

willMoveToSuperview是属于UIView的函数

当(被addSubview,比如:

self.view.addSubview(userTextField)

)时,会被调用,此时newSuperview是新的parent view

另外,也会在:被从父视图删除掉时,被调用,此时

willMoveToSuperview中,你可以去实现一些自定义的逻辑newSuperview为nil

用法,此处用于文本框中,用于添加监测事件:

//
//  NormalTextField.swift
//  JianDao
//
//  Created by licrifan on 16/3/18.
//  Copyright © 2016 licrifan. All rights reserved.
//
import UIKit
class NormalTextField: UITextField, UITextFieldDelegate {
    let borderColor:(active: UIColor, inactive: UIColor) = ( UIColor(hexString: "#288CDE")!, UIColor(hexString: "#D1D1D1")!)
   
。。。
    override func willMoveToSuperview(newSuperview: UIView!) {
        print("willMoveToSuperview newSuperview=\(newSuperview)")
        if newSuperview != nil {
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFieldDidEndEditing", name:UITextFieldTextDidEndEditingNotification, object: self)
           
            NSNotificationCenter.defaultCenter().addObserver(self, selector: "textFieldDidBeginEditing", name:UITextFieldTextDidBeginEditingNotification, object: self)
        } else {
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
    }
   
    func textFieldDidBeginEditing() {
        print("textFieldDidBeginEditing")
        self.layer.borderColor = borderColor.active.CGColor
    }
   
    func textFieldDidEndEditing() {
        print("textFieldDidEndEditing")
        self.layer.borderColor = borderColor.inactive.CGColor
        self.resignFirstResponder()
    }
}

转载请注明:在路上 » [已解决]swift willMoveToSuperview的含义

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
60 queries in 0.095 seconds, using 18.68MB memory