简体   繁体   English

将UITableView方向更改为从右到左

[英]Change UITableView direction to Right-to-Left

I develop an iOS application and my native language is Persian (a Right-to-Left language). 我开发了一个iOS应用程序,我的母语是波斯语(一种从右到左的语言)。 How can I change UITableView direction to right-to-left in iOS programming? 如何在iOS编程中将UITableView方向更改为从右到左?

A UITableView with RTL direction has: 具有RTL方向的UITableView具有:

  • Icons are at right 图标在右边
  • TableCell detail and button are at left TableCell详细信息和按钮位于左侧
  • TableView header and footer are right-aligned TableView页眉和页脚是右对齐的

like this : 像这样 :

在此输入图像描述

Is there any UI or programming way to do this? 是否有任何UI或编程方式来执行此操作?

Try this on Swift 3 在Swift 3上试试吧

tableView.semanticContentAttribute = .forceRightToLeft

or in StoryBoard Cell properties (Semantic dropdown) select "Force Right-To-Left" 或者在StoryBoard Cell属性(Semantic dropdown)中选择“Force Right-to-Left”

在此输入图像描述

我知道它会自iOS8以来自动改为从右到左。

You can try to do this as well 您也可以尝试这样做

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = myTableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell

    cell.textLabel?.textAlignment = .Right

    cell.textLabel?.text = goalsArray[indexPath.row]

    return cell
}

Build your view's constraints with NSLayoutAttributeLeading and NSLayoutAttributeTrailing instead of NSLayoutAttributeLeft and NSLayoutAttributeRight . 使用NSLayoutAttributeLeadingNSLayoutAttributeTrailing而不是NSLayoutAttributeLeftNSLayoutAttributeRight构建视图的约束。

Your view's Direction will be the same with your app's language Direction. 您的视图的方向将与您应用的语言方向相同。

在此输入图像描述

for me this way worked 对我来说这种方式很有效

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = self.tableView.dequeueReusableCell(withIdentifier: cellReuseIdentifier)! as UITableViewCell

    //cell.myView.backgroundColor = self.colors[indexPath.row]
    cell.textLabel?.text =
    "\((self.FAQs?[indexPath.row].Question)!)"

    cell.textLabel?.textAlignment = .right

    return cell
}

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

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