我的iOS应用仅限阿拉伯语(从右向左)。在iOS 9之前,视图布局和视图动画都是默认从左到右。所以,我已经定制了完整的应用程序并且已经改变了默认行为,例如导航栏中的后退按钮设置为右侧而不是默认左侧。
但是现在当使用最新的SDK(Xcode 7 beta 4)编译应用程序时,一切都与我需要的相反。
是否有任何简单的方法可以强制应用程序显示视图并且行为类似于iOS 8和7?
我搜索并找到了解决方案,但它涉及更改所有视图的约束(取消选中“尊重语言方向”)。但这在大型项目中不是一个可行的解决方案。
答案 0 :(得分:22)
编辑:以下代码可能会导致意外行为,因为@wakachamo指出。所以,请注意问题,例如交互式弹出手势不起作用,警报视图不显示等。如果这对您不起作用,最好按照@wakachamo提供的说明进行操作
将此添加到app delegate didFinishLaunchingWithOptions方法。
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
还添加guard以支持早期版本,以便该属性不会导致任何崩溃。
if([[[UIView alloc] init] respondsToSelector:@selector(setSemanticContentAttribute:)]) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
}
[[UIView alloc] init]而不是[UIView外观],因为respondsToSelector当前没有使用[UIView appearance] for setSemanticContentAttribute。您还可以添加iOS 9检查
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
}
答案 1 :(得分:16)
我从Apple Developer Forums找到了正确的方法
此处iOS 9 beta - UIAlertController is not working
只需添加此代码即可使alertview与之协同工作
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"9.0")) {
[[UIView appearance] setSemanticContentAttribute:UISemanticContentAttributeForceLeftToRight];
[[UIView appearanceWhenContainedIn:[UIAlertController class], nil] setSemanticContentAttribute:UISemanticContentAttributeUnspecified];
[[UIView appearanceWhenContainedIn:[UIAlertView class], nil] setSemanticContentAttribute:UISemanticContentAttributeUnspecified];
}
答案 2 :(得分:11)
我知道回答太迟了。但我已经弄明白了(只有导航栏问题)。
答案改编自@Kamran Kan。感谢@Kamran。
Jus在@Kamran的回答中用 UINavigationBar 替换 UIView 。然后代码如下。
char *args[] = {
"ubus", "-S", "call", "network.interface", "status", "{\"interface\": \"lan\"}", NULL
};
execvp("ubus", args);
由于它只影响UINavigationBar,其他控件不会起作用。所以UIAlertView工作得很好......
答案 3 :(得分:7)
没有简单的方法。建议您尽可能长期迁移到标准API。
另一种方法是将所有受影响的视图的semanticContentAttribute
设置为UISemanticContentAttributeForceLeftToRight
,但这与将所有约束设置为使用左/右而不是前导/尾随一样可行。除此之外,如果您要定位iOS< 9,还必须围绕可用性检查来处理这些来电。
答案 4 :(得分:5)
SWIFT 2 代码
if #available(iOS 9.0, *) {
myView.semanticContentAttribute = .ForceLeftToRight
}
答案 5 :(得分:4)
SWIFT 3
if #available(iOS 10.0, *) {
UIView.appearance().semanticContentAttribute = .forceLeftToRight
}
答案 6 :(得分:3)