Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在IOS 8中设置UIAlertController的高度和宽度_Objective C_Iphone_Xcode_Ipad_Ios8 - Fatal编程技术网

Objective c 如何在IOS 8中设置UIAlertController的高度和宽度

Objective c 如何在IOS 8中设置UIAlertController的高度和宽度,objective-c,iphone,xcode,ipad,ios8,Objective C,Iphone,Xcode,Ipad,Ios8,我有一个带有文本文件的UIAlertController。 问题是默认的UIAlertController的大小非常小。它的文本无法正常查看 因此,我想增加UIAlertController的高度和宽度。换句话说,我想创建一个自定义UIAlertController。这样做的方法是什么?我认为您无法设置大小。错误的解决方法是在消息上设置\n。UIAlertView也有同样的限制 我建议使用UIPopoverController并实现您自己的discouse按钮,因为UIAlertControll

我有一个带有文本文件的UIAlertController。 问题是默认的UIAlertController的大小非常小。它的文本无法正常查看


因此,我想增加UIAlertController的高度和宽度。换句话说,我想创建一个自定义UIAlertController。这样做的方法是什么?

我认为您无法设置大小。错误的解决方法是在消息上设置
\n
。UIAlertView也有同样的限制

我建议使用UIPopoverController并实现您自己的
discouse
按钮,因为UIAlertController的目的更多的是根据Apple文档向用户显示警报消息(短消息)。我不认为信息墙可以被视为警告信息

一般来说,我认为这一限制是由苹果设定的,作为提醒。这个视图是向用户显示短消息,作为用户体验的一部分

使用示例代码编辑 首先,对不起,我的意思是
UIPopoverPresentViewController
,而不是
UIPopoverController

下面是示例类:

@interface DemoPopOverPresentViewController : UIViewController

- (instancetype)initWithTitle:(NSString*)title message:(NSString*)message buttonTitle:(NSString*)buttonTitle;

@property NSString* titleText;
@property NSString* messageText;
@property NSString* buttonTitleText;

@property UILabel* titleLabel;
@property UILabel* textLabel;
@property UIButton* submitButton;

@end

@implementation DemoPopOverPresentViewController

- (instancetype)initWithTitle:(NSString*)title message:(NSString*)message buttonTitle:(NSString*)buttonTitle;
{
    self = [super init];

    if ( self ) {
        _titleText = title;
        _messageText = message;
        _buttonTitleText = buttonTitle;
    }

    return self;
}

- (void)viewDidLoad;
{
    [super viewDidLoad];

    _titleLabel = [UILabel new];
    [_titleLabel setTextAlignment:NSTextAlignmentCenter];
    [_titleLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]];
    [_titleLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_titleLabel setText:_titleText];
    [self.view addSubview:_titleLabel];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_titleLabel]|"     options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleLabel)]];

    _textLabel = [UILabel new];
    [_textLabel setTextAlignment:NSTextAlignmentCenter];
    [_textLabel setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
    [_textLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
    [_textLabel setNumberOfLines:0];
    [_textLabel setText:_messageText];
    [self.view addSubview:_textLabel];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_textLabel]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_textLabel)]];

    _submitButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [_submitButton setTitle:_buttonTitleText forState:UIControlStateNormal];
    [_submitButton addTarget:self action:@selector(submitButtonTouched:) forControlEvents:UIControlEventTouchUpInside];
    [_submitButton setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view addSubview:_submitButton];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"|[_submitButton]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_submitButton)]];

    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-[_titleLabel(<=44.0)]-16-[_textLabel]-16-[_submitButton(<=44.0)]-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_titleLabel,_textLabel,_submitButton)]];
}

- (void)submitButtonTouched:(id)sender;
{
    [self dismissViewControllerAnimated:YES completion:^{

    }];
}

@end
  • UIPopoverPresentationControllerDelegate
    -(UIModalPresentationStyle)自适应PresentationStyleforPresentationController:(UIPresentationController*)控制器实现委托方法,并返回
    UIModalPresentationNone


  • 我知道这已关闭,但我发现您可以像这样向alertviewcotnroller.view添加约束

    var height:NSLayoutConstraint = NSLayoutConstraint(item: alertController.view, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: self.view.frame.height * 0.80)
        alertController.view.addConstraint(height);
    

    这肯定对你有帮助。(Swift 4.2)


    您可以通过约束控制UIAlertController

    假设我们希望此警报范围更广:

    如果查看视图层次结构,您将看到UIKit将UIAlertController宽度限制为270:

    检查此视图子项,您可以发现其第一个子项也具有此优先级为998的约束

    知道我们可以在呈现警报控制器之前更新此约束。大概是这样的:

        let alert = UIAlertController(title: "Some gorgeous very big big big title with many words", message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a augue eget magna maximus posuere. Donec pellentesque lacus ut tellus mollis, eget congue nulla dapibus. Sed pharetra porta lorem, ac faucibus risus scelerisque vitae. Aenean lacinia lobortis quam quis finibus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed odio nisl, pretium a turpis in, pulvinar bibendum augue. Proin id ligula elementum, pulvinar lorem et, suscipit turpis. Duis in tortor arcu. Donec in dapibus ex.\n\nDuis sit amet lacus nec mauris blandit dignissim. Sed efficitur vestibulum sapien ut condimentum. Donec a lorem sit amet augue imperdiet dictum sed eu sapien. Donec in congue quam, vitae luctus augue. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vivamus felis ipsum, malesuada eu dictum non, imperdiet ut urna. Vivamus tempus ante sit amet quam interdum feugiat. Ut at nulla nibh.", preferredStyle: .alert)
    
        alert.addAction(UIAlertAction(title: "Hide this", style: .default, handler: nil))
        // Filtering width constraints of alert base view width
        let widthConstraints = alert.view.constraints.filter({ return $0.firstAttribute == .width })
        alert.view.removeConstraints(widthConstraints)
        // Here you can enter any width that you want
        let newWidth = UIScreen.main.bounds.width * 0.90
        // Adding constraint for alert base view
        let widthConstraint = NSLayoutConstraint(item: alert.view,
                                                 attribute: .width,
                                                 relatedBy: .equal,
                                                 toItem: nil,
                                                 attribute: .notAnAttribute,
                                                 multiplier: 1,
                                                 constant: newWidth)
        alert.view.addConstraint(widthConstraint)
        let firstContainer = alert.view.subviews[0]
        // Finding first child width constraint
        let constraint = firstContainer.constraints.filter({ return $0.firstAttribute == .width && $0.secondItem == nil })
        firstContainer.removeConstraints(constraint)
        // And replacing with new constraint equal to alert.view width constraint that we setup earlier
        alert.view.addConstraint(NSLayoutConstraint(item: firstContainer,
                                                    attribute: .width,
                                                    relatedBy: .equal,
                                                    toItem: alert.view,
                                                    attribute: .width,
                                                    multiplier: 1.0,
                                                    constant: 0))
        // Same for the second child with width constraint with 998 priority
        let innerBackground = firstContainer.subviews[0]
        let innerConstraints = innerBackground.constraints.filter({ return $0.firstAttribute == .width && $0.secondItem == nil })
        innerBackground.removeConstraints(innerConstraints)
        firstContainer.addConstraint(NSLayoutConstraint(item: innerBackground,
                                                        attribute: .width,
                                                        relatedBy: .equal,
                                                        toItem: firstContainer,
                                                        attribute: .width,
                                                        multiplier: 1.0,
                                                        constant: 0))
    
        present(alert, animated: true, completion: nil)
    
    现在,您的警报将占据90%的屏幕:


    现在我只找到了这个解决方案。可能会有更优雅、更安全的解决方案,但我想你已经明白了。

    只需更新宽度约束值即可。

    // calculate new width
    let newWidth = UIScreen.main.bounds.width * 0.90 - 270
    
    // update width constraint value for main view
    if let viewWidthConstraint = alertController.view.constraints.filter({ return $0.firstAttribute == .width }).first{
        viewWidthConstraint.constant = newWidth
    }
    
    // update width constraint value for container view
    if let containerViewWidthConstraint = alertController.view.subviews.first?.constraints.filter({ return $0.firstAttribute == .width }).first {
        containerViewWidthConstraint.constant = newWidth
    }
    
    //present alertController
    self.present(alertController, animated: true, completion: nil)
    

    是的,我知道这是一个很好的方法来显示一个长消息的UIPopoverController,但客户端需要UIAlertController。然后,你可以使用UIPopoverController来模拟警报窗口的UI。你能给我一个示例代码或任何对我有帮助的教程吗?我能告诉你的Popover的问题是它必须指向它的来源,与屏幕上刚刚出现的警报不同,我也只能在高度上使用它。只适用于iPhone。iPad带有“无法同时满足约束”的字样。因此,如果苹果向iPhone添加约束,那么同样的问题也会发生。@Jonathan Ellis您可以使用相同的方式为宽度添加约束,如下所示:*var width:NSLayoutConstraint=NSLayoutConstraint(项:alertController.view,属性:NSLayoutAttribute.Width,relatedBy:NSLayoutRelation.Equal,toItem:nil,属性:NSLayoutAttribute.NotaAttribute,乘数:1,常数:self.view.frame.Width*0.80)alertController.view.addConstraint(宽度);**在我的项目中,我将其用于宽度控制,它起作用了..如何设置AlertViewControllerLever的宽度想法,但不幸的是,对我不起作用。也许uialertcontroller视图的层次结构在ios13中发生了变化。
    let alert = UIAlertController(title: "Alert", message: "Some message", preferredStyle: .actionSheet)
    
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) in
    
    }))
    alert.addAction(UIAlertAction(title: "Some", style: .default))
    alert.addAction(UIAlertAction(title: "Some", style: .default))
    
    let height:NSLayoutConstraint = NSLayoutConstraint(item: alert.view, attribute: NSLayoutConstraint.Attribute.height, relatedBy: NSLayoutConstraint.Relation.equal, toItem: nil, attribute: NSLayoutConstraint.Attribute.notAnAttribute, multiplier: 1, constant: view.frame.height)
    alert.view.addConstraint(height);
    present(alert, animated: true)
    
        let alert = UIAlertController(title: "Some gorgeous very big big big title with many words", message: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam a augue eget magna maximus posuere. Donec pellentesque lacus ut tellus mollis, eget congue nulla dapibus. Sed pharetra porta lorem, ac faucibus risus scelerisque vitae. Aenean lacinia lobortis quam quis finibus. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Sed odio nisl, pretium a turpis in, pulvinar bibendum augue. Proin id ligula elementum, pulvinar lorem et, suscipit turpis. Duis in tortor arcu. Donec in dapibus ex.\n\nDuis sit amet lacus nec mauris blandit dignissim. Sed efficitur vestibulum sapien ut condimentum. Donec a lorem sit amet augue imperdiet dictum sed eu sapien. Donec in congue quam, vitae luctus augue. Interdum et malesuada fames ac ante ipsum primis in faucibus. Vivamus felis ipsum, malesuada eu dictum non, imperdiet ut urna. Vivamus tempus ante sit amet quam interdum feugiat. Ut at nulla nibh.", preferredStyle: .alert)
    
        alert.addAction(UIAlertAction(title: "Hide this", style: .default, handler: nil))
        // Filtering width constraints of alert base view width
        let widthConstraints = alert.view.constraints.filter({ return $0.firstAttribute == .width })
        alert.view.removeConstraints(widthConstraints)
        // Here you can enter any width that you want
        let newWidth = UIScreen.main.bounds.width * 0.90
        // Adding constraint for alert base view
        let widthConstraint = NSLayoutConstraint(item: alert.view,
                                                 attribute: .width,
                                                 relatedBy: .equal,
                                                 toItem: nil,
                                                 attribute: .notAnAttribute,
                                                 multiplier: 1,
                                                 constant: newWidth)
        alert.view.addConstraint(widthConstraint)
        let firstContainer = alert.view.subviews[0]
        // Finding first child width constraint
        let constraint = firstContainer.constraints.filter({ return $0.firstAttribute == .width && $0.secondItem == nil })
        firstContainer.removeConstraints(constraint)
        // And replacing with new constraint equal to alert.view width constraint that we setup earlier
        alert.view.addConstraint(NSLayoutConstraint(item: firstContainer,
                                                    attribute: .width,
                                                    relatedBy: .equal,
                                                    toItem: alert.view,
                                                    attribute: .width,
                                                    multiplier: 1.0,
                                                    constant: 0))
        // Same for the second child with width constraint with 998 priority
        let innerBackground = firstContainer.subviews[0]
        let innerConstraints = innerBackground.constraints.filter({ return $0.firstAttribute == .width && $0.secondItem == nil })
        innerBackground.removeConstraints(innerConstraints)
        firstContainer.addConstraint(NSLayoutConstraint(item: innerBackground,
                                                        attribute: .width,
                                                        relatedBy: .equal,
                                                        toItem: firstContainer,
                                                        attribute: .width,
                                                        multiplier: 1.0,
                                                        constant: 0))
    
        present(alert, animated: true, completion: nil)
    
    // calculate new width
    let newWidth = UIScreen.main.bounds.width * 0.90 - 270
    
    // update width constraint value for main view
    if let viewWidthConstraint = alertController.view.constraints.filter({ return $0.firstAttribute == .width }).first{
        viewWidthConstraint.constant = newWidth
    }
    
    // update width constraint value for container view
    if let containerViewWidthConstraint = alertController.view.subviews.first?.constraints.filter({ return $0.firstAttribute == .width }).first {
        containerViewWidthConstraint.constant = newWidth
    }
    
    //present alertController
    self.present(alertController, animated: true, completion: nil)