我有一个iPhone应用程序,点击按钮时应该打开一个自定义警报视图,显示一些文本和右上角带有“X”交叉符号的窗口,就像我们在任何Web应用程序中都有的灯箱一样。
我有一个iPhone应用程序,点击按钮时应该打开一个自定义警报视图,显示一些文本和右上角带有“X”交叉符号的窗口,就像我们在任何Web应用程序中都有的灯箱一样。
这里有一个链接,我认为它可以解决你的问题。
http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-uialertview-custom-graphics/
http://iosdevtricks.blogspot.com/2013/04/creating-custom-alert-view-for-iphone.html
如果您想要实现自定义警告视图,那么您应该使用这个示例代码,它具有非常吸引人的警告视图集合,并借助ViewController进行操作。 请尝试此示例代码链接 https://github.com/eaigner/CODialog
在ReturnAlert.xib中创建自定义视图
class ReturnView: UIView, UITextFieldDelegate {
}
class ReturnAlert: UIAlertController {
var returnView: ReturnView?
public convenience init(id message: String?) {
self.init(title: nil, message: "message", preferredStyle: .alert)
guard let view = UINib(nibName: "ReturnAlert", bundle: Bundle(for: ReturnView.self)).instantiate(withOwner: ReturnView.self, options: nil)[0] as? ReturnView else{
fatalError("Return view not found")
}
self.returnView = view
view.alert = self
let parent = self.view.subviews[0].subviews[0]
for sv in parent.subviews {
sv.removeFromSuperview()
}
view.bounds = CGRect(x: parent.bounds.origin.x, y: parent.bounds.origin.y, width: view.frame.size.width, height: view.frame.size.height)
parent.addSubview(view)
let xConstraint = NSLayoutConstraint(item: view, attribute: .centerX, relatedBy: .equal, toItem: parent, attribute: .centerX, multiplier: 1, constant: 0)
let yConstraint = NSLayoutConstraint(item: view, attribute: .centerY, relatedBy: .equal, toItem: parent, attribute: .centerY, multiplier: 1, constant: 0)
parent.addConstraint(xConstraint)
parent.addConstraint(yConstraint)
}
}
- (void)willPresentAlertView:(UIAlertView *)alertView;
- (void)didPresentAlertView:(UIAlertView *)alertView;
for (UIView* view in [actionSheet subviews])
{
NSLog(@"%@",[view class]);
if ([[[view class] description] isEqualToString:@"UIAlertButton"] && [view respondsToSelector:@selector(setAlpha:)])
{
[view setAlpha:2.0];
[view setOpaque:YES];
if ([view respondsToSelector:@selector(title)])
{
NSString* title = [view performSelector:@selector(title)];
if ([title isEqualToString:@"Cancel"] && [view respondsToSelector:@selector(setBackgroundImage:forState:)] && [view respondsToSelector:@selector(setFrame:)] && [view respondsToSelector:@selector(setFrame:)] && [view respondsToSelector:@selector(setTitleColor:forState:)])
{
[view setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[view setBackgroundImage:[UIImage imageNamed:@"btn-cancel.png"] forState:UIControlStateNormal];
[view setFrame:CGRectMake(view.frame.origin.x, view.frame.origin.y+10, view.frame.size.width,view.frame.size.height)];
}
}
}
}