iOS- How can I get UIAlertView width with customize and put the UILabel to center

家住魔仙堡 提交于 2019-12-11 04:46:02

问题


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

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

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

How can I get the alertView's width?

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

.h file:

 #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 file:

   #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;
 }

Thank you very much.


回答1:


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.

Just remember:

  1. UIAlertView is not meant to be subclassed.
  2. As of iOS 8, UIAlertView is deprecated and you should use UIAlertController.



回答2:


You should not subclass UIAlertView, APPLE has strictly said no from iOS 7 onwards. All you have to do is create a custom UIView, add UITableView as subview and so on based on your requirement.

UIAlertController is new with iOS 8, so adopting it may sound good for iOS 8 only but for iOS 7 it would be problem.

Better do UIView subclassing instead of UIAlertView.



来源:https://stackoverflow.com/questions/28535868/ios-how-can-i-get-uialertview-width-with-customize-and-put-the-uilabel-to-cente

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!