iOS-如何通过自定义获取UIAlertView宽度并将UILabel居中 [英] iOS- How can I get UIAlertView width with customize and put the UILabel to center

查看:129
本文介绍了iOS-如何通过自定义获取UIAlertView宽度并将UILabel居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自定义了 UIAlertView ,我需要向 alertView 中添加一些组件(如标签,表格视图)。

I was custom the UIAlertView, I need add some component to the alertView (like label, tableview).

但是我有一个问题。我需要知道alertView的宽度。

But I have a question. I need to know the alertView's width.

我尝试添加 self.frame.size.width ,但是我刚得到 0.000000

I try to add self.frame.size.width, but I just got the 0.000000.

如何获取alertView的宽度?

How can I get the alertView's width?

我将计算将子视图放在,并将 UILabel 放在子视图的中心。我的下面的部分代码:

I will calculate to put the subview at, and put the UILabel to subview to center. My partial code below:

.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

.m文件:

   #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 。我在此处找到了相当不错的教程,它会遍历查找警报视图的有效边界和插图的过程,从而使您能够找到宽度。

Although subclassing UIAlertView may (and most likely) will result in the rejection of your app by Apple, it's still possible. You will have to override layoutSubviews and drawRect. I've found fairly good tutorial here, which iterates through the process of finding the active bounds and inset of the alert view, which would allow you to find the width.

只需记住:


  1. UIAlertView并不打算被子类化。

  2. 从iOS 8开始,UIAlertView已弃用,您应该使用UIAlertController。

这篇关于iOS-如何通过自定义获取UIAlertView宽度并将UILabel居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆