开发者

A better UIAlert dialog

开发者 https://www.devze.com 2022-12-21 13:30 出处:网络
I have seen several apps that display some form of alert which sometimes require user entry or displays some type of information.Anyway, judging by the designs of the \"alert\", I would assume they a开

I have seen several apps that display some form of alert which sometimes require user entry or displays some type of information. Anyway, judging by the designs of the "alert", I would assume they a开发者_运维知识库re using something else to display these.

My first guess was some form of view but I see that they are smaller in size and are centered in the main view. Is this still possibly a custom view? Are there any examples of this where a custom view is displayed modal and require dismissal or acknowledgment before continuing?

As always, thanks in advance for any info.

Geo...


If you want a truly custom UIAlertView replacement, I created a UIView subclass with the style if a UIAlertView as a demonstration of Quartz drawing, for which the source code can be found here. The only thing missing is the custom button type for the alert view.

However, be careful in the use of nonstandard alert views. The ones provided for you are designed to be consistent, so that they user has a uniform experience across the iPhone OS. If you build things that differ from the rest of the OS, it could lead to frustration when people use your application.


To create a custom alert which acts modally, you could do the following:

Create a UIView with two subviews. One of those sub-UIViews covers the entire window (e.g., 320x480 in size) with an adjusted alpha value so that it displays the content under it dimmed. The sibling subview is the container for your Alert and should be frontmost.

Ignore events in the dimmer view, respond appropriately to taps or other events in the alert container view.

When all the views are configured and layed-out, add the top view as subview to something like [[UIApplication sharedApplication]keyWindow]

[[[UIApplication sharedApplication]keyWindow] addSubview:myCustomAlertView];

NOTE: the reason for the sibling views where one is dimmed and thwarts UI events is I'm pretty certain that if you lower the alpha of a superview, the child views inherit that property. AT least I recall having dealt with this issue in the past. I'll stand corrected if wrong.

-- I created a custom alert class which, while not modal at the moment, dismisses automatically after a number of seconds, or before the timeout if a touchUpInside event is received. It's just a UIView which responds to taps. I found that there were several areas in my app where I wanted to show a brief message, but came to dislike having to always hit an "OK" button to move on. With this class, I can move on with other activities even before the alert auto-hides, or still just tap to lose it quickly.


Without having any example, it's hard to say exactly what is being used. But, in most cases, UIActionSheet is used for such custom dialogs.

Class Reference.


Often people are using a UIAlertView and they just put a bunch of newlines in the message field. This will give you the height you want depending on # of newlines. Then they add any additional views by calling addSubview:viewToAdd. I know this is a pain and a "hack" but its become a pretty common method for people to do. Here is a forum post on the topic which illustrates how to do it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号