iOS-如何通过自定义获取UIAlertView宽度并将UILabel置于中心
我自定义了iOS-如何通过自定义获取UIAlertView宽度并将UILabel置于中心,ios,center,uialertview,Ios,Center,Uialertview,我自定义了UIAlertView,我需要向alertView添加一些组件(如标签、tableview) 但我有一个问题。我需要知道alertView的宽度 我尝试添加self.frame.size.width,但我刚得到0.000000 如何获取alertView的宽度 我将计算将子视图放置在,并将子视图的UILabel放置在中间。我的部分代码如下: .h文件: #import <UIKit/UIKit.h> @interface CustomTableViewAlert :
UIAlertView
,我需要向alertView
添加一些组件(如标签、tableview)
但我有一个问题。我需要知道alertView的宽度
我尝试添加self.frame.size.width
,但我刚得到0.000000
如何获取alertView的宽度
我将计算将子视图放置在,并将子视图的UILabel
放置在中间。我的部分代码如下:
.h文件:
#import <UIKit/UIKit.h>
@interface CustomTableViewAlert : UIAlertView
-(instancetype)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;
@end
#import "CustomTableViewAlert.h"
...
@implementation CustomTableViewAlert
-(instancetype) initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...
{
self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
if( self )
{
NSLog(@"=====self.frame.size.width:%f",self.frame.size.width);
// ^^^^^^^^^^^^^^ I got 0.000000
CGSize screenSize = [UIScreen mainScreen].bounds.size;
float showInAlertWidth = screenSize.width - 100;
float showInAlertHeight = 150;
CGSize textSize = [[notFoundDeviceLabel text] sizeWithAttributes:@{NSFontAttributeName:[notFoundDeviceLabel font]}];
CGFloat strikeWidth = textSize.width;
CGFloat strikeHeight = textSize.height;
[notFoundDeviceLabel setFrame:CGRectMake(0, 0, screenSize.width, showInAlertHeight)];
notFoundDeviceLabel.text = @"No Device found";
notFoundDeviceLabel.textAlignment = NSTextAlignmentCenter;
// new container view
containerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenSize.width, showInAlertHeight)];
containerView.backgroundColor = [UIColor yellowColor];
[containerView addSubview: notFoundDeviceLabel];
}
return self;
}
非常感谢。尽管将UIAlertView子类化可能(而且最有可能)会导致Apple拒绝您的应用程序,但仍然有可能。您必须覆盖
layoutSubviews
和drawRect
。我已经找到了相当好的教程,它迭代了查找活动边界和警报视图插入的过程,这将允许您查找宽度
记住:
你不应该将UIAlertView作为子类,苹果从iOS 7开始就严格拒绝了。您所要做的就是创建一个自定义UIView,将UITableView添加为子视图,等等 UIAlertController是iOS 8的新版本,所以采用它可能只对iOS 8有好处,但对iOS 7来说可能会有问题
最好使用UIView子类化而不是UIAlertView。阅读
UIAlertView
的文档。它根本不是子类或修改。它声明:“UIAlertView类旨在按原样使用,不支持子类化。该类的视图层次结构是私有的,不能修改。”除了@rmaddy所说的,文档还声明:“重要信息:UIAlertView在iOS 8中不推荐使用。(请注意,UIAlertViewDelegate也不推荐使用。)要在iOS 8及更高版本中创建和管理警报,请改用首选样式为UIAlertControllerStyleAlert的UIAlertController。“UIAlertController可以添加UITableView吗?谢谢。UIAlertController也是如此,“UIAlertController类旨在按原样使用,不支持子类化。此类的视图层次结构是私有的,不能修改。“因此,我应该自定义一个类似alertview的UIView,以及视图中的自定义弹出窗口吗?”@@I只想显示弹出窗口,以便用户选择蓝牙设备。有什么好方法(使用导航页除外)?是使用自定义UIView,添加一个具有一定透明度的完整视图,这样它将阻止与viewcontroller的交互,除非alertview被取消。在此基础上,添加另一个UIView(添加UITableview+取消,确定按钮)。处理按钮操作时,使用一些代理来查找选定的单元格。对不起,UIAlertController能达到效果吗(包含uitableView)?谢谢,@ibiren。因为我认为要自定义UIView,我们必须控制UIView是否关闭。它变得很复杂。我认为您可以子类化,但它只能在iOS 8设备中工作。