這聽起來像你基本上試圖在iOS上重新創建一個「Toast」。好消息是,有人已經這樣做了。 See this project.
編輯:不想使用iToast。我喜歡你的風格,更少的代碼。這是我想出的。這似乎很明顯,因爲其他人已經表示,要克服UIAlertView
的模態性質,唯一的方法是添加超級視圖來處理觸摸事件。但是您不必每次都手動執行此操作,請考慮子類UIAlertView
。嘗試是這樣的:
編輯: @wagashi,感謝您接受我的回答,並感謝元首上漲約setFrame:
是一個好地方,調整大小。你的代碼確實做了一個非常敬酒的小警戒,但是當我嘗試它時,我發現如果消息太長,視圖似乎分崩離析。因此,我修改了setFrame:
,只是將警報的大小縮小了一個按鈕的大小,並保持居中在屏幕上。所以班級準確地回答問題標題「iOS如何在任何地方輕輕一點地解除UIAlertView?」
NoButtonAlertView.h
#import <UIKit/UIKit.h>
@interface _NoButtonAlertViewCover : UIView
@property (nonatomic,assign) UIAlertView *delegate;
@end
@interface NoButtonAlertView : UIAlertView
-(id)initWithTitle:(NSString *)title message:(NSString *)message;
@end
NoButtonAlertView.m
#import "NoButtonAlertView.h"
@implementation _NoButtonAlertViewCover
@synthesize delegate = _delegate;
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
[self removeFromSuperview];
[_delegate dismissWithClickedButtonIndex:0 animated:YES];
}
@end
@implementation NoButtonAlertView
-(void)show{
[super show];
_NoButtonAlertViewCover *cover = [[_NoButtonAlertViewCover alloc] initWithFrame:[UIScreen mainScreen].bounds];
cover.userInteractionEnabled = YES;
cover.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.01];
cover.delegate = self;
[self.superview addSubview:cover];
}
-(id)initWithTitle:(NSString *)title message:(NSString *)message{
if ((self = [super initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil])){
}
return self;
}
- (void)setFrame:(CGRect)rect {
// Called multiple times, 4 of those times count, so to reduce height by 40
rect.size.height -= 10;
self.center = self.superview.center;
[super setFrame:rect];
}
@end
有了這個簡單UIAlertView
子及其UIView
子類覆蓋,你可以簡單地,你會用它標準UIAlertView
。像這樣:
NoButtonAlertView *alert = [[NoButtonAlertView alloc] initWithTitle:@"Hello" message:@"I am the very model of a modern major general; I'm information, vegitable, animal, and mineral."];
[alert show];
將產生: