objective-c 自定义 UISwitch 和 App Store 批准

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/694848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:19:00  来源:igfitidea点击:

Custom UISwitch & App Store approval

iphoneobjective-c

提问by pix0r

After doing some reading, I've found that you can customize the text and color on a UISwitch control. I'm curious if these methods will cause problems trying to get my app approved and included in the App Store.

阅读一些文章后,我发现您可以自定义 UISwitch 控件上的文本和颜色。我很好奇这些方法是否会导致尝试让我的应用程序获得批准并将其包含在 App Store 中时出现问题。

Sample code taken from iPhone Developer's Cookbook Sample Code:

取自iPhone Developer's Cookbook Sample Code 的示例代码

// Custom font, color
switchView = [[UICustomSwitch alloc] initWithFrame:CGRectZero];
[switchView setCenter:CGPointMake(160.0f,260.0f)];
[switchView setLeftLabelText: @"Foo"];
[switchView setRightLabelText: @"Bar"];
[[switchView rightLabel] setFont:[UIFont fontWithName:@"Georgia" size:16.0f]];
[[switchView leftLabel] setFont:[UIFont fontWithName:@"Georgia" size:16.0f]];
[[switchView leftLabel] setTextColor:[UIColor yellowColor]]; 

采纳答案by zpesk

this will not create problems with submitting to the app store. You are allowed to use custom controls or altered versions of the built in controls so long as you do not use any private (undocumented) APIs to build/alter these widgets.

这不会在提交到应用商店时产生问题。只要您不使用任何私有(未记录的)API 来构建/更改这些小部件,您就可以使用自定义控件或内置控件的更改版本。

回答by Robert Chin

You might enjoy my implementation of a custom switch (open source and free to use)... it allows setting the switch to display any text, or easily subclass it to draw your own custom images in the track.... http://osiris.laya.com/projects/rcswitch/

您可能会喜欢我对自定义开关的实现(开源且免费使用)...它允许将开关设置为显示任何文本,或轻松将其子类化以在轨道中绘制您自己的自定义图像.... http:/ /osiris.laya.com/projects/rcswitch/

This switch makes it easy to draw an image instead of text: subclass the main switch class and override the draw method like this, and your image will be automatically animated:

这个开关可以很容易地绘制图像而不是文本:子类化主开关类并像这样覆盖 draw 方法,您的图像将自动动画:

- (void)drawUnderlayersInRect:(CGRect)aRect withOffset:(float)offset inTrackWidth:(float)trackWidth
{
    [onImage drawAtPoint:CGPointMake(floorf(aRect.origin.x + 13 + (offset - trackWidth)), floorf((self.bounds.size.height - onImage.size.height) / 2.0))];
    [offImage drawAtPoint:CGPointMake(floorf(aRect.origin.x + 15.0 + (offset + trackWidth)), ceilf((self.bounds.size.height - offImage.size.height) / 2.0))];
}

回答by infiniteLoop

No need to UISwitch subclass at all. A fairly simple solution which I Implemented is subclased UIView and on touch event toggled between two images (ON/OFF) with slide transition thats all.

根本不需要 UISwitch 子类。我实现的一个相当简单的解决方案是子类 UIView 和触摸事件在两个图像(开/关)之间切换,滑动过渡就是这样。

Regards Dhanesh

问候达内什

回答by eiran

i just created this view, and saw you question

我刚刚创建了这个视图,看到你的问题

hope this helps

希望这可以帮助

the .h file:

.h 文件:

#import <UIKit/UIKit.h>

@interface EDSwitch : UIView
{
    UIButton* onButton,*offButton;
    UIImageView* bg;
}

- (id)initWithText:(NSString*)on andText:(NSString*)off andDelegate:(id)delegate andOnSelector:(SEL)onSelector andOffSelector:(SEL)offSelector andBackgroundImage:(UIImage*)bgImage andStartingValue:(BOOL)b;

@end

and the .m file:

和 .m 文件:

#import "EDSwitch.h"

@implementation EDSwitch

- (id)initWithText:(NSString*)on andText:(NSString*)off andDelegate:(id)delegate     andOnSelector:(SEL)onSelector andOffSelector:(SEL)offSelector andBackgroundImage:     (UIImage*)bgImage andStartingValue:(BOOL)b
{
self = [super initWithFrame:CGRectZero];
if (self) {
    UILabel* onLabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 8, 50, 20)];
    onLabel.text = on ;
    onLabel.tag = 1;
    onLabel.font = [UIFont fontWithName:kCalibri size:15];
    onLabel.textAlignment = UITextAlignmentCenter;
    onLabel.textColor = [UIColor colorFromHexString:@"#009dd0"];
    onLabel.backgroundColor = [UIColor clearColor];
    [onLabel sizeToFit];
    [onLabel setWidth:onLabel.frame.size.width + 4];


    UILabel* offLabel = [[UILabel alloc] initWithFrame:CGRectMake(2, 8, 50, 20)];
    offLabel.text = off ;
    offLabel.tag = 1;
    offLabel.textAlignment = UITextAlignmentCenter;
    offLabel.font = [UIFont fontWithName:kCalibri size:15];
    offLabel.textColor = [UIColor colorFromHexString:@"#009dd0"];
    offLabel.backgroundColor = [UIColor clearColor];
    [offLabel sizeToFit];
    [offLabel setWidth:offLabel.frame.size.width + 4];

    float high = MAX([offLabel.text sizeWithFont:offLabel.font].width,[onLabel.text sizeWithFont:onLabel.font].width) + 10;

    onButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [onButton addTarget:self action:@selector(toggled:) forControlEvents:UIControlEventTouchUpInside];
    [onButton addTarget:delegate action:onSelector forControlEvents:UIControlEventTouchUpInside];
    offButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [offButton addTarget:self action:@selector(toggled:) forControlEvents:UIControlEventTouchUpInside];
    [offButton addTarget:delegate action:offSelector forControlEvents:UIControlEventTouchUpInside];

    [onButton setWidth:high];
    [onButton setX:0];
    [onButton addSubview:onLabel];
    [onLabel setWidth:high];
    [onLabel setX:0];

    [offButton setWidth:high];
    [offButton addSubview:offLabel];
    [offButton setX:high];
    [offLabel setWidth:high];
    [offLabel setX:0];

    bg = [[UIImageView alloc] initWithImage:bgImage];

    self.frame = CGRectMake(200, 200 , (high*2), 34);
    self.layer.borderColor = [[[UIColor colorFromHexString:@"#009dd0"] colorWithAlphaComponent:0.5] CGColor];
    self.layer.borderWidth = 0.5;
    self.layer.cornerRadius = 5;
    [self setX:[UIApplication sharedApplication].keyWindow.frame.size.width - self.frame.size.width - 8];

    [self addSubview:bg];

    [bg setWidth:[self getWidth]];
    [bg setHeight:[self getHeight]];

    [self addSubview:onButton];
    [self addSubview:offButton];

    [onButton setHeight:[self getHeight]];
    [offButton setHeight:[self getHeight]];

    if(b){
        [onButton setBackgroundColor:[UIColor clearColor]];
        [offButton setBackgroundColor:[UIColor whiteColor]];
    }
    else{
        [onButton setBackgroundColor:[UIColor whiteColor]];
        [offButton setBackgroundColor:[UIColor clearColor]];
    }
}
return self;
}

-(void)toggled:(UIButton*)sender{
if(sender == onButton){
    UILabel* l = (UILabel*)[onButton viewWithTag:1];
    l.textColor = [UIColor grayColor];
    [onButton setBackgroundColor:[UIColor clearColor]];
    l = (UILabel*)[offButton viewWithTag:1];
    l.textColor = [UIColor colorFromHexString:@"#009dd0"];
    [offButton setBackgroundColor:[UIColor whiteColor]];

}
else{
    UILabel* l = (UILabel*)[offButton viewWithTag:1];
    l.textColor = [UIColor grayColor];
    [offButton setBackgroundColor:[UIColor clearColor]];
    l = (UILabel*)[onButton viewWithTag:1];
    l.textColor = [UIColor colorFromHexString:@"#009dd0"];
    [onButton setBackgroundColor:[UIColor whiteColor]];
}
}

@end

usage:

用法:

    [[UIApplication sharedApplication].keyWindow addSubview:[[EDSwitch alloc] initWithText:@"aksdjaksdjh" andText:@"dasjdsaj" andDelegate:self andOnSelector:@selector(logon) andOffSelector:@selector(logoff) andBackgroundImage:[UIImage imageNamed:@"toggleBottom.png"] andStartingValue:YES]];

live long and prosper,

健康长寿·繁荣昌盛,

eiran

永兰

回答by uk_rules_ok

From Apple documentation :-

来自 Apple 文档:-

You use the UISwitch class to create and manage the On/Off buttons you see, for example, in the preferences (Settings) for such services as Airplane Mode. These objects are known as switches.

The UISwitch class declares a property and a method to control its on/off state. As with UISlider, when the user manipulates the switch control (“flips” it) a UIControlEventValueChanged event is generated, which results in the control (if properly configured) sending an action message.

The UISwitch class is not customisable.

您可以使用 UISwitch 类来创建和管理您看到的开/关按钮,例如,在诸如飞行模式之类的服务的首选项(设置)中。这些对象称为开关。

UISwitch 类声明了一个属性和一个方法来控制其开/关状态。与 UISlider 一样,当用户操作开关控件(“翻转”它)时,会生成 UIControlEventValueChanged 事件,这会导致控件(如果配置正确)发送操作消息。

UISwitch 类不可定制。

Apple is saying that they aren't customisable, which might mean having your app rejected.

Apple 表示它们不可定制,这可能意味着您的应用程序被拒绝。

回答by Nic Rodgers

In the Apple Human Interface Guidelines, in the Switch documentation, Apple state:

在 Apple Human Interface Guidelines 的 Switch 文档中,Apple 声明:

Use a switch in a table row to give users two simple, diametrically opposed choices that determine the state of something, such as yes/no or on/off. Use a predictable pair of values so that users don't have to slide the control to discover what the other value is.

使用表格行中的开关为用户提供两个简单的、截然相反的选择,以确定某事的状态,例如是/否或开/关。使用一对可预测的值,这样用户就不必滑动控件来发现另一个值是什么。

So, yes, it's fine to change the text so long as you use a predictable pair of values (like yes/no).

所以,是的,只要您使用一对可预测的值(如是/否),就可以更改文本。

回答by Nic Rodgers

This WILL cause issues with your app approval. ask me how I know :P

这将导致您的应用程序审批出现问题。问我怎么知道的:P

I just got a nasty note today from Apple. We are in the process of looking for an alternative.

我今天刚收到一封来自 Apple 的讨厌的便条。我们正在寻找替代方案。