将UITableView方向更改为从右到左

时间:2016-06-26 06:02:10

标签: ios swift uitableview right-to-left

我开发了一个iOS应用程序,我的母语是波斯语(一种从右到左的语言)。如何在iOS编程中将UITableView方向更改为从右到左?

具有RTL方向的UITableView具有:

  • 图标在右侧
  • TableCell详细信息和按钮位于左侧
  • TableView页眉和页脚是右对齐的
像这样:

enter image description here

是否有任何用户界面或编程方式可以执行此操作?

5 个答案:

答案 0 :(得分:20)

在Swift 3上试试这个

tableView.semanticContentAttribute = .forceRightToLeft

或在StoryBoard Cell属性(Semantic dropdown)中选择“Force Right-to-Left”

enter image description here

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您也可以尝试这样做

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
}

答案 3 :(得分:0)

使用NSLayoutAttributeLeadingNSLayoutAttributeTrailing代替NSLayoutAttributeLeftNSLayoutAttributeRight来构建视图的约束。

您的观看方向与您应用的语言方向相同。

enter image description here

答案 4 :(得分:0)

对我来说这种方式有效

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
}