1. 程式人生 > >iOS 子檢視 父檢視 UIView 相關的方法

iOS 子檢視 父檢視 UIView 相關的方法

初始化一個檢視
- (id)initWithFrame:(CGRect)aRect //用指定的frame 初始化一個檢視物件
結束檢視中的編輯狀態
- (BOOL)endEditing:(BOOL)force  
//這個在彈回鍵盤時特別有用
關於響應
- (void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
//為檢視新增一個手勢識別器
- (void)removeGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
//移除一個手勢識別器
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
//開始一個手勢識別器
- (void)addMotionEffect:(UIMotionEffect *)effect
//開始向檢視中新增運動效果,這麼說其實比較模糊,比如說警告框會隨著手機的傾斜而傾斜,
當然這需要程式碼實現,只是我們現在可以檢測手機的運動事件來做出相應的響應
//從IOS7開始使用
管理檢視層次 Managing the View Hierarchy
- (void)addSubview:(UIView *)view //新增子檢視
- (void)removeFromSuperview //從父檢視中移除
- (void)bringSubviewToFront:(UIView *)view 
//移動指定的子檢視到最頂層
- (void)sendSubviewToBack:(UIView *)view
//移動制定的子檢視到後方,所有子檢視的下面
- (void)insertSubview:(UIView *)view atIndex:(NSInteger)index
//在指定的位置插入子檢視,檢視的所有檢視其實組成了一個數組
- (void)insertSubview:(UIView *)view aboveSubview:(UIView *)siblingSubview
//將指定的子檢視移動到指定siblingSubview子檢視的前面
- (void)insertSubview:(UIView *)view belowSubview:(UIView *)siblingSubview
//將指定的子檢視移動到指定siblingSubview子檢視的後面
- (void)exchangeSubviewAtIndex:(NSInteger)index1 withSubviewAtIndex:(NSInteger)index2
//交換兩子檢視的位置
- (BOOL)isDescendantOfView:(UIView *)view
//判斷接收物件是否是指定檢視的子檢視,或與指定檢視是同一檢視
配置自動調整大小狀態 Configuring the Resizing Behavior
- (void)sizeToFit //根據子檢視的大小位置,調整檢視,使其恰好圍繞子檢視,
也就是說自動適應子檢視的大小,只顯示子檢視
- (CGSize)sizeThatFits:(CGSize)size
//讓檢視計算最適合子檢視的大小,即能把全部子檢視顯示出來所需要的最小的size
鋪設子檢視 Laying out Subviews
- (void)layoutSubviews //勾畫子檢視
- (void)layoutIfNeeded //立即勾畫子檢視
- (void)setNeedsLayout 
//使當前接收物件的佈局作廢,並在下一更新週期觸發一個新的佈局
選擇加入基於約束的佈局 Opting in to Constraint-Based Layout
- (void)setTranslatesAutoresizingMaskIntoConstraints:(BOOL)flag
//設定檢視自動調整尺寸的掩碼是否轉化為基於約束佈局的約束
- (BOOL)translatesAutoresizingMaskIntoConstraints
//判斷檢視是否將自動調整尺寸的掩碼是否轉化為基於約束佈局的約束
管理約束 Managing Constraints
- (NSArray *)constraints //返回由檢視建立的約束- (void)addConstraint:(NSLayoutConstraint *)constraint
//為檢視佈局或者子檢視新增一個約束,constraint約束只能在當前檢視範圍內,包括子檢視
- (void)addConstraints:(NSArray *)constraints
//新增一組約束
- (void)removeConstraint:(NSLayoutConstraint *)constraint
//移除檢視上指定的約束
- (void)removeConstraints:(NSArray *)constraints
//移除指定的一組約束
在基於約束佈局的測量 Measuring in Constraint-Based Layout
- (CGSize)systemLayoutSizeFittingSize:(CGSize)targetSize
//返回滿足持有約束的檢視的size
- (CGSize)intrinsicContentSize
//返回接收物件的原本大小
- (void)invalidateIntrinsicContentSize
//廢除檢視原本內容的size
- (UILayoutPriority)contentCompressionResistancePriorityForAxis:(UILayoutConstraintAxis)axis
//設定當檢視要變小時,檢視的壓縮改變方式,是水平縮小還是垂直縮小,並返回一個優先權
- (void)setContentCompressionResistancePriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
//設定優先權
- (UILayoutPriority)contentHuggingPriorityForAxis:(UILayoutConstraintAxis)axis
//與上邊相反是檢視的放大改變方式
- (void)setContentHuggingPriority:(UILayoutPriority)priority forAxis:(UILayoutConstraintAxis)axis
//參照上面的方法
校準檢視 Aligning Views with Constraint-Based Layout
- (CGRect)alignmentRectForFrame:(CGRect)frame
//返回給定框架的檢視的對齊矩陣
- (CGRect)frameForAlignmentRect:(CGRect)alignmentRect
//返回給定對齊矩形的檢視的frame
- (UIEdgeInsets)alignmentRectInsets
//返回從檢視的frame上定義的對齊矩陣的邊框 
- (UIView *)viewForBaselineLayout
//返回滿足基線約束條件的檢視
觸發基於約束的佈局 Triggering Constraint-Based Layout
- (BOOL)needsUpdateConstraints //檢視的約束是否需要更新
- (void)setNeedsUpdateConstraints //設定檢視的約束需要更新
- (void)updateConstraints //為檢視更新約束
- (void)updateConstraintsIfNeeded //更新檢視和其子檢視的約束
除錯基於約束的佈局 Debugging Constraint-Based Layout
- (NSArray *)constraintsAffectingLayoutForAxis:(UILayoutConstraintAxis)axis
//返回影響一個給定軸檢視佈局的約束
- (BOOL)hasAmbiguousLayout //檢視的位置是否不完全指定
- (void)exerciseAmbiguityInLayout
//在不同的有效值之間用一個模糊的佈局隨機改變檢視的frame
繪畫和更新檢視 Drawing and Updating the View
- (void)drawRect:(CGRect)rect //在指定的區域繪畫檢視
- (void)setNeedsDisplay //標記整個檢視的邊界矩形需要重繪
- (void)setNeedsDisplayInRect:(CGRect)invalidRect
//標記在指定區域內的檢視的邊界需要重繪
格式化列印檢視內容
- (UIViewPrintFormatter *)viewPrintFormatter //返回檢視的列印格式化
- (void)drawRect:(CGRect)area forViewPrintFormatter:(UIViewPrintFormatter *)formatter
//指定區域和列印格式繪畫檢視內容
儲存和恢復狀態 Preserving and Restoring State
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
//編碼檢視的狀態資訊
- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
//解碼一個檢視狀態資訊
標記檢視  
- (UIView *)viewWithTag:(NSInteger)tag
轉換檢視間座標 Converting Between View Coordinate Systems
- (CGPoint)convertPoint:(CGPoint)point toView:(UIView *)view
//轉換一個點從接受物件的座標系到指定檢視
- (CGPoint)convertPoint:(CGPoint)point fromView:(UIView *)view
//與上面相反,指定檢視座標中的一個點轉換為接收物件
- (CGRect)convertRect:(CGRect)rect toView:(UIView *)view
- (CGRect)convertRect:(CGRect)rect fromView:(UIView *)view
//參照上面兩個方法
檢視中的點選測試
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
//在指定點上點選測試指定事件
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
//測試指定的點是否包含在接收物件中
注意與檢視相關聯的改變 Observing View-Related Changes
- (void)didAddSubview:(UIView *)subview
//通知檢視指定子檢視已經新增
- (void)willRemoveSubview:(UIView *)subview
//通知檢視將要移除指定的子檢視
- (void)willMoveToSuperview:(UIView *)newSuperview
//通知檢視將要移動到一個新的父檢視中
- (void)didMoveToSuperview
//通知檢視已經移動到一個新的父檢視中
- (void)willMoveToWindow:(UIWindow *)newWindow
//通知檢視將要移動到一個新的window中
- (void)didMoveToWindow
//通知檢視已經移動到一個新的window中

相關推薦

iOS 檢視 檢視 UIView 相關方法

初始化一個檢視- (id)initWithFrame:(CGRect)aRect //用指定的frame 初始化一個檢視物件結束檢視中的編輯狀態- (BOOL)endEditing:(BOOL)force  //這個在彈回鍵盤時特別有用關於響應- (void)addGestureRecognizer:(UIG

iOS中超出檢視的按鈕點選事件響應處理

在iOS開發中會遇到一些設計樣式,需要把按鈕一部分懸空在父檢視的上面,但是當我們點選該按鈕時,超出了父檢視的懸空部分不會響應該按鈕的點選事件。 原理就是iOS的touch事件的相應是從最下方的父檢視開始的,系統判斷點選的座標點上沒有子檢視,所以不再響應,該原理可參見之前寫過

ios button超出檢視如何響應事件

 今天中午吃飯的時候,公司一哥們突然問了我一句,怎麼做一個超出tabBar高度的按鈕,而且還能響應事件,吃完飯之後,我思索了一下,決定動手試試,其實邏輯很簡單,只需要繼承UITabBarContro

iOS開發~檢視超過檢視範圍的事件響應問題

當按鈕超過了父檢視範圍,點選是沒有反應的。因為訊息的傳遞是從最下層的父檢視開始呼叫hittest方法。 - (UIView *)hitTest:(CGPoint)point withEvent:(UI

iOS檢視捕獲到的touches如何讓檢視響應

用代理實現最簡單了,子檢視宣告一個protocol和與之對應的method和delegate property,然後父檢視實現。例如:子檢視SonView.h:@class SonView; //這個必不可少,因為真正的class interface在下面宣告,而在protocol宣告中又要用到這個class

ios檢視檢視同時處理輸入事件

在使用UITableView巢狀UICollectionView的過程中, collectionView作為cell,我希望,點選collectionView的讓tableView對應的行也被選中。 如果直接判斷collectionView所在的行,然後設定cell.sel

iOS 檢視超出檢視不響應解決辦法

父檢視中重寫該方法 Objective-C - (nullable UIView *)hitTest:(CGPoint)point withEvent:(nullable UIEvent *)event { UIView * view = [super hitTe

ios 手勢點選檢視的時候不響應檢視的點選事件

手勢點選子檢視的時候不響應父檢視的點選事件; 一句程式碼搞定: if( CGRectContainsPoint(_BGView.frame, [sender locationInView:_

iOS----在檢視中獲取檢視的控制器

在實際專案當中,我們經常會遇到需要在一個父類為UIView的子檢視上進行POP或是PUSH操作,但該子檢視是不能用POP或PUSH的,這時候我們就可以根據該子檢視獲取到父檢視的控制器,從而進行POP或P

ios獲取檢視檢視檢視的座標

例如:self.view中add了一個tableView,tableViewCell中添加了一個textField,想要獲得textField在self.view中的座標。 CGRect textFrame = [textField.superview convertRec

檢視A超出檢視B範圍點選觸發檢視A中的點選事件

Demo 解決示例 1、定義子檢視 // .h檔案 #import <UIKit/UIKit.h> @interface ButtonView : UIView @property (nonatomic, copy) void (^buttonClick)(UI

iOS 添加了一個view,然後發現一點選事件就跑到檢視

用另外一個JWViewController.view 新增到當前檢視的self.view上面,發現點選檢視上面的按鈕,直接響應的是self.view的事件,一看是JWViewController上面的view超出了螢幕,而在self.view裡邊移動JWViewController.view的時候,超出JWV

iOS檢視控制器的跳轉方法

檢視的跳轉有很多的方式,本文主要選擇常用的方式並將其分為普通的viewController和導航控制器navigationController兩種來進行詳細說明 一、普通檢視控制器跳轉 對於普通的檢視

使用通知機制,在dismissViewControllerAnimated後,completion傳值給上一個檢視方法

檢視firstView和secendView,點選firstView上面的按鈕presentviewcontroller出secendView;secendView上有個按鈕,點選按鈕dismissViewControllerAnimated,並將某個值傳給firstVi

iOS裝置模擬器檔案檢視方法

模擬器檔案檢視 好,這個時候選擇往模擬器上面除錯程式: 執行模擬器; 開啟Finder,按住option,在選單欄中選擇“前往”-》“資源庫” 如果沒發現資源庫,則使用終端 命令列輸入  open ~/Library/ 預設給隱藏了 然後接著選擇第一個

iOS 返回檢視之後如何重新整理頁面

  view B tableview中的cell,點選後push到了view A。view A pop回來的時候,我想reload view B中的tableview 在B檢視 -(void)viewDidAppear:(BOOL)animated{} 這個方法裡面重新[s

如何讓超出檢視區域的檢視實現點選事件

在父檢視重寫hitTest:withEvent:方法,程式碼如下: - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

iOS工作中的問題-----navigationBar透明方法、遮擋UIViewController、UITableViewController 檢視解決方法

導航欄透明方法: UIImage *image = [UIImage imageNamed:@"clear"]; [self.navigationController.navigationBar setBackgroundImage:imag

Masonry的使用之利用檢視撐起檢視

之前的一篇文章(https://www.jianshu.com/p/9563f5b646d9)講過使用Masonry佈局讓Cell的高度實現自適應,其實這就是通過子檢視撐起了父檢視Cell的高度。今天再

vue組件獲取組件的屬性或方法

class col ont hang 提交按鈕 代碼 emp ref input 我遇到這樣一個情況: 父組件代碼片段: 1 <button @click="submit">提交</button> 2 <v-autoTextarea ref=