天天看點

iOS開發UI篇—popoverController使用注意

ios開發ui篇—popovercontroller使用注意

一、設定尺寸

提示:不建議,像下面這樣吧popover的寬度和高度寫死。

iOS開發UI篇—popoverController使用注意
iOS開發UI篇—popoverController使用注意

更好的設計是:popover的尺寸應該由内部控制器的内容所決定。

内容控制器可以自行設定自己在popover中顯示的尺寸,其中有兩種方法:

(1)在ios 7之前  @property (nonatomic,readwrite) cgsize contentsizeforviewinpopover;

(2)從ios 7開始  @property (nonatomic) cgsize preferredcontentsize;

  以上屬性都是uiviewcontroller的

iOS開發UI篇—popoverController使用注意
iOS開發UI篇—popoverController使用注意

效果:

  

iOS開發UI篇—popoverController使用注意

關于min(a,b)的說明,最終的大小取決于b,但是最大不能超過a,如果超過a那麼值就等于a。

二、設定顯示的位置

1.設定顯示的位置有2種方法

(1)圍繞着一個uibarbuttonitem顯示(箭頭指定那個uibarbuttonitem)

- (void)presentpopoverfrombarbuttonitem:(uibarbuttonitem *)item permittedarrowdirections:(uipopoverarrowdirection)arrowdirections animated:(bool)animated;

item :圍繞着哪個uibarbuttonitem顯示

arrowdirections :箭頭的方向

animated :是否通過動畫顯示出來

(2)圍繞着某一塊特定區域顯示(箭頭指定那塊特定區域)

- (void)presentpopoverfromrect:(cgrect)rect inview:(uiview *)view permittedarrowdirections:(uipopoverarrowdirection)arrowdirections animated:(bool)animated;

rect :指定箭頭所指區域的矩形框範圍(位置和尺寸),以view的左上角為坐标原點

view :rect參數是以view的左上角為坐标原點(0,0)

rect和view參數如下:

iOS開發UI篇—popoverController使用注意

相關代碼:

iOS開發UI篇—popoverController使用注意
iOS開發UI篇—popoverController使用注意

界面效果:(部分)

iOS開發UI篇—popoverController使用注意

關于frame坐标計算的圖示:

iOS開發UI篇—popoverController使用注意
iOS開發UI篇—popoverController使用注意

下面兩者是等價的:

iOS開發UI篇—popoverController使用注意

即如果想讓箭頭指向某一個uiview的做法有2種做法,比如指向一個button

方法1

  [popover presentpopoverfromrect:button.bounds inview:button permittedarrowdirections:uipopoverarrowdirectiondown animated:yes];

方法2

  [popover presentpopoverfromrect:button.frame inview:button.superview permittedarrowdirections:uipopoverarrowdirectiondown animated:yes];

三、設定代理

代理對象

  @property (nonatomic, assign) id <uipopovercontrollerdelegate> delegate;

是否可見

  @property (nonatomic, readonly, getter=ispopovervisible) bool popovervisible;

箭頭方向

  @property (nonatomic, readonly) uipopoverarrowdirection popoverarrowdirection; 

關閉popover(讓popover消失)

  - (void)dismisspopoveranimated:(bool)animated;

代碼說明:

iOS開發UI篇—popoverController使用注意
iOS開發UI篇—popoverController使用注意

四、防止點選uipopovercontroller區域外消失

預設情況下

隻要uipopovercontroller顯示在螢幕上,uipopovercontroller背後的所有控件預設是不能跟使用者進行正常互動的

點選uipopovercontroller區域外的控件,uipopovercontroller預設會消失

要想點選uipopovercontroller區域外的控件時不讓uipopovercontroller消失,解決辦法是設定passthroughviews屬性

@property (nonatomic, copy) nsarray *passthroughviews;

這個屬性是設定當uipopovercontroller顯示出來時,哪些控件可以繼續跟使用者進行正常互動。這樣的話,點選區域外的控件就不會讓uipopovercontroller消失了

代碼示例:

iOS開發UI篇—popoverController使用注意
iOS開發UI篇—popoverController使用注意

補充:

uipopovercontroller這個類是隻能用在ipad中的

要想在iphone中實作popover效果,必須得自定義view,可以參考

<a target="_blank" href="http://code4app.com/ios/popover-view-in-iphone/4fa931bd06f6e78d0f000000">http://code4app.com/ios/popover-view-in-iphone/4fa931bd06f6e78d0f000000</a>

<a target="_blank" href="http://code4app.com/ios/popup-menu/512231ac6803fa9e08000000">http://code4app.com/ios/popup-menu/512231ac6803fa9e08000000</a>

繼續閱讀