【问题标题】:Is customizing UIAlertView is allowed by Apple? [closed]Apple 是否允许自定义 UIAlertView? [关闭]
【发布时间】:2011-11-07 17:24:52
【问题描述】:

我正准备通过继承 UIAlerView 来提交这样的自定义。它的布局完全基于 UIAlertView 的给定地形,没有读取任何私有属性。 App Store 审核流程是否接受这种定制?

BGAlertViewWithSwitch.h

//
//  BGAlertViewWithSwitch.h
//  BGAlertViewWithSwitch
//
//  Created by Borbas Geri on 11/7/11.
//  Copyright 2011 ©ompactApps. All rights reserved.
//

#import <Foundation/Foundation.h>


//An assumed value.
#define ALERT_VIEW_LINE_HEIGHT 20.0
#define ALERT_VIEW_LABEL_PADDING 5.0
#define ALERT_VIEW_LABEL_ALPHA 0.5

#define kAlertSwitchLabelTag 42


@interface BGAlertViewWithSwitch : UIAlertView 
{
    UISwitch *_alertSwitch;
    UILabel *_alertSwitchLabel;
}
@property (nonatomic, retain) UISwitch *alertSwitch;
@property (nonatomic, retain) UILabel *alertSwitchLabel;
@property (nonatomic, readonly, getter=isOn) BOOL on;

-(id)initWithTitle:(NSString*) title
           message:(NSString*) message
     switchMessage:(NSString*) switchMessage
          delegate:(id) delegate
 cancelButtonTitle:(NSString*) cancelButtonTitle
     okButtonTitle:(NSString*) okButtonTitle;

@end

BGAlertViewWithSwitch.m

//
//  BGAlertViewWithSwitch.m
//  BGAlertViewWithSwitch
//
//  Created by Borbas Geri on 11/7/11.
//  Copyright 2011 ©ompactApps. All rights reserved.
//


#import "BGAlertViewWithSwitch.h"


@implementation BGAlertViewWithSwitch
@synthesize alertSwitch = _alertSwitch;
@synthesize alertSwitchLabel = _alertSwitchLabel;


#pragma mark - UISwitch Accessor

-(BOOL)isOn
{
    return self.alertSwitch.isOn;
}


#pragma mark - View lifecycle

-(id)initWithTitle:(NSString*) title
           message:(NSString*) message
     switchMessage:(NSString*) switchMessage
          delegate:(id) delegate
 cancelButtonTitle:(NSString*) cancelButtonTitle
     okButtonTitle:(NSString*) okButtonTitle
{

    //For testing layout
    NSString *placeHolder = @"";

    //Append a line to the message that leaves the place for the switch. 
    NSString *_expandedMessage = [NSString stringWithFormat:@"%@\n%@\n%@\n", message, placeHolder, placeHolder];

    if (self = [self initWithTitle:title
                           message:_expandedMessage
                          delegate:delegate
                 cancelButtonTitle:cancelButtonTitle
                 otherButtonTitles:okButtonTitle, nil])
    {
        //Add switch.
        self.alertSwitch = [[UISwitch alloc] init];
        self.alertSwitch.on = YES; 
        [self addSubview:self.alertSwitch];

        //Add label.
        self.alertSwitchLabel = [[UILabel alloc] init];
        self.alertSwitchLabel.text = switchMessage;
        self.alertSwitchLabel.tag = kAlertSwitchLabelTag;
        [self addSubview:self.alertSwitchLabel];
    }
    return self;
}

- (void)dealloc
{
    self.alertSwitch = nil;
    self.alertSwitchLabel = nil;

    [super dealloc];
}


#pragma mark - Topography

- (void)layoutSubviews
{
    NSLog(@"layoutSubviews to (%@)", NSStringFromCGRect(self.frame));

    //Weak link to the message label.
    UILabel *messageLabel;

    //Enumerate subviews to find message label (the base of the topography).
    for (UIView *eachSubview in self.subviews)
        if ([[eachSubview class] isEqual:[UILabel class]])
        {
            UILabel *eachLabel = (UILabel*)eachSubview;
            if (eachLabel.tag != kAlertSwitchLabelTag)
            {
                messageLabel = eachLabel;
                NSLog(@"Each label frame (%@), saying '%@'", NSStringFromCGRect(eachLabel.frame), eachLabel.text);                
            }
        }

    //Center new content.
    CGSize alertSwitchLabelSize = [self.alertSwitchLabel.text sizeWithFont:messageLabel.font];
    float horizontalCentering = (messageLabel.frame.size.width - (alertSwitchLabelSize.width + ALERT_VIEW_LABEL_PADDING + self.alertSwitch.frame.size.width)) / 2;


    //Switch goes to the bottom right.
    float switchVerticalCentering = ((ALERT_VIEW_LINE_HEIGHT * 2 + 1) - self.alertSwitch.frame.size.height ) / 2;
    CGRect alertSwitchFrame = CGRectMake(messageLabel.frame.origin.x + messageLabel.frame.size.width - self.alertSwitch.frame.size.width - horizontalCentering,
                                         messageLabel.frame.origin.y + messageLabel.frame.size.height - self.alertSwitch.frame.size.height - switchVerticalCentering,
                                         self.alertSwitch.frame.size.width,
                                         self.alertSwitch.frame.size.height);
    self.alertSwitch.frame = alertSwitchFrame;

    //Label goes to the bottom left.    
    float switchLabelVerticalCentering = ((ALERT_VIEW_LINE_HEIGHT * 2 + 1) - ALERT_VIEW_LINE_HEIGHT ) / 2;
    CGRect alertSwitchLabelFrame = CGRectMake(round( messageLabel.frame.origin.x + horizontalCentering ),
                                              round( messageLabel.frame.origin.y + messageLabel.frame.size.height - ALERT_VIEW_LINE_HEIGHT - switchLabelVerticalCentering ),
                                              messageLabel.frame.size.width - self.alertSwitch.frame.size.width,
                                              ALERT_VIEW_LINE_HEIGHT); //self.alertSwitchLabel.frame.size.height);
    self.alertSwitchLabel.frame = alertSwitchLabelFrame;

    //Copy message label properties.
    self.alertSwitchLabel.backgroundColor = [UIColor clearColor];   
    self.alertSwitchLabel.textColor = messageLabel.textColor;
    self.alertSwitchLabel.font = messageLabel.font;
    self.alertSwitchLabel.shadowColor = messageLabel.shadowColor;
    self.alertSwitchLabel.shadowOffset = messageLabel.shadowOffset;

    //Weaken.
    self.alertSwitchLabel.alpha = ALERT_VIEW_LABEL_ALPHA;

    [super layoutSubviews];
}


@end

【问题讨论】:

  • 嘿,只是一个问题。我可以在我的项目中使用你的代码吗?我需要完全相同的东西。非常感谢

标签: ios app-store uialertview appstore-approval


【解决方案1】:

这个问题的实际答案是——Apple 不允许UIAlertView 被子类化。来自 UIAlertView 文档:

子类化注释

UIAlertView 类旨在按原样使用,而不是 支持子类化。此类的视图层次结构是私有的,并且 不得修改。

在这里找到: Subclassing UIAlertView

【讨论】:

    【解决方案2】:

    只有 Apple 才能充分回答这个问题,所以最好的办法是对其进行测试。我认为您必须问自己的主要问题是:我是否违反了 Apple 开发者协议中的任何条款?如果没有,请提交您的应用程序。如果您担心被拒绝,请考虑另一种可以完成此操作的方法,作为备用,并准备好在出现问题时提交。

    不是你问的,但我也认为苹果设计的这种变化不是很直观。你的意思是开关意思是“也从 moquus 中删除”?因为您那里已经有一个大的删除按钮。如果开关关闭,那么删除按钮删除了什么?

    【讨论】:

    • 太好了,谢谢。我将包括“也”这个词。顺便说一句,我只是将这个页面发布到 Apple 并提出一个问题,所以我希望它会尽快回答。
    • 我更改了上面截图中的单词。 :)
    • 是的,这不是太“自然”,但比从头开始创建 UIAlertView 要好。
    • 为什么不添加第三个按钮? "Cancel" "Delete Moquus and moqu.us" "Delete this Moquu" -- 或者其他更好的措辞,这取决于 Moquus 是什么。
    • 这感觉有点重复。另一种(也许是最好的)方法可能是在 App 设置中单独设置此选项,并且只有一个小标签显示 UIAlertView 中的实际状态(“Moquus 也将从 moqu.us 中删除”,或相反)。这种意图不会经常改变。
    猜你喜欢
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 2013-04-29
    • 1970-01-01
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多