简体   繁体   English

在UIAlertView中向下移动按钮

[英]Move buttons down in UIAlertView

I have an app which displays in landscape mode and I've overwritten the height of a UIAlertView with the following code: 我有一个以横向模式显示的应用程序,并且用以下代码覆盖了UIAlertView的高度:

- (void) willPresentAlertView:(UIAlertView *)alertView
{
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    CGRect frame = [alertView frame];
    alertView.frame = CGRectMake(frame.origin.x, 0, frame.size.width, screenBounds.size.width);
}

This almost works. 这几乎可行。 The UIAlertView is taller, however, the buttons in the alertview don't get pushed down. UIAlertView较高,但是alertview中的按钮不会被按下。

Does anyone know how I can push the buttons in a UIAlertView down when I change the alert view's height? 有谁知道当我更改警报视图的高度时如何按下UIAlertView的按钮?

I think it is more elegant and less risky to replace UIAlertView with some independent AlertView instead of messing around with it.With independent I mean not inheriting form UIAlertView. 我认为用一些独立的AlertView代替UIAlertView而不是搞乱UIAlertView更加优雅,风险也更低。
TSAlertView is such a replacement. TSAlertView就是这样的替代品。

http://dl.dropbox.com/u/47535/TSAlertView/1-thumb.png http://dl.dropbox.com/u/47535/TSAlertView/3-thumb.png http://dl.dropbox.com/u/47535/TSAlertView/2-thumb.png http://dl.dropbox.com/u/47535/TSAlertView/1-thumb.png http://dl.dropbox.com/u/47535/TSAlertView/3-thumb.png http://dl.dropbox。 com / u / 47535 / TSAlertView / 2-thumb.png

NSArray *subViewArray=[alertView subviews]; NSArray * subViewArray = [alertView子视图];

for (NSUInteger ind=0 ; ind < [subViewArray count]; ind++) {

    if ([[alertView.subviews objectAtIndex:ind] isKindOfClass:[UIButton class]]) {
        UIButton* button1 = (UIButton*)[alertView.subviews objectAtIndex:ind];
        [button1 setFrame:CGRectMake(button1.frame.origin.x, button1.frame.origin.y+100, button1.frame.size.width, button1.frame.size.height)];
        NSLog(@"button1.frame.origin.y=[%1.0f]",button1.frame.origin.y);

    }
}

I'm guessing that you'll probably have to subclass / manipulate the UIAlertView class to achieve that, which can be risky as Apple can change their implementations so wrap your code in appropriate checks. 我猜想您可能必须子类化/操作UIAlertView类才能实现这一点,因为Apple可以更改其实现,然后将代码包装在适当的检查中,所以这样做可能会有风险。

A start would be to do: 一个开始就是:

NSLog(@"UIAlertView subviews: %@",alertView.subviews);

That'll print some output for the various elements making up the UIAlertView, you will probably see a few UIButtons in there which you can then manipulate by using something like: 这将为组成UIAlertView的各种元素打印一些输出,您可能会在其中看到一些UIButton,然后可以使用类似的操作:

UIButton* button1 = (UIButton*)[alertView.subviews objectAtIndex:N];
[button1 setFrame:CGRect(button1.frame.origin.x, button1.frame.origin.y+10, button1.frame.size.width, button1.frame.size.height)]

A sensible check would be to confirm that the objectAtIndex is the correct type of element before you perform operations on it, this isn't foolproof however as Apple could add more UIButtons or something.. 一个明智的检查是在执行操作之前确认objectAtIndex是正确的元素类型,但这并不是万无一失的,但是由于Apple可以添加更多UIButton或其他内容。

if ([[alertView.subviews objectAtIndex:N] isKindOfClass:[UIButton class]]) {
  ...
}

Depending on your situation you may also want to iterate over the subviews array and move all UIButtons down rather than just hardcoding in some specific objectsAtIndex but that's a whole other answer. 根据您的情况,您可能还需要遍历subviews数组并将所有UIButton向下移动,而不只是对某些特定的objectAtIndex进行硬编码,但这是另一个答案。 :-) :-)

This link should help. 该链接应该有所帮助。 In my opinion, I would not try messing around with the view hierarchies. 我认为,我不会尝试弄乱视图层次结构。 This can lead to rejection from the App Store. 这可能导致被App Store拒绝。 Either build a new AlertView from scratch, or leave it as it is. 从头开始构建新的AlertView,或者保持原样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM