【问题标题】:Make view round after autolayout自动布局后使视图圆形
【发布时间】:2015-08-01 15:43:23
【问题描述】:

我有一个 tableview 带有自定义单元格,这些单元格是从 storyboard 构建的,标识符使用 AutoLayout

其中一个subviews需要是圆形的(layer.cornerRadius = width/2),开头是正方形。

我在layoutSubviews() 中尝试过,但它似乎在AutoLayout 更改其大小之前被调用...didMoveToSuperview() 也是如此

AutoLayout 更改大小后,将此类内容更新到我的子视图的正确功能在哪里?

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell_small") as! Cell
    ...
    return cell
}

// In Cell

override func layoutSubviews() {
    rankLabel.layer.cornerRadius = rankLabel.bounds.width/2
    rankLabel.layer.masksToBounds = true
}

override func didMoveToSuperview() {
    rankLabel.layer.cornerRadius = rankLabel.bounds.width/2
    rankLabel.layer.masksToBounds = true
}

结果:

【问题讨论】:

  • 你试过cellForRowAtIndexPath方法吗?
  • cellForRowAtIndexPath 在布局之前被调用,看我下面的回答,你可以使用 willDisplayCell
  • 使用 rankLabel.clipsToBounds=YES;
  • 如果我打印出 rankLabel 的尺寸,这对于我尝试过的所有东西来说都是错误的尺寸。但是如果我滑动表格视图,那么它会调用 updatefunctions 并且大小是正确的。
  • 如果我想要一个圆形子视图,我之前在自动布局方面遇到了很多问题......

标签: ios uiview autolayout


【解决方案1】:

你有两个选择:

  1. 创建自定义类 YourCell 并将代码添加到 initWithCoder-Method
  2. 给你的 ViewController 添加一个方法

喜欢关注

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath

并在那里改变它。访问您调用的单元格

[(YourCell *)cell view]....

【讨论】:

  • 两者都不起作用:(而且我认为iniWithCoder永远不会起作用,因为它在其他任何东西之前被调用:P
【解决方案2】:

尝试像这样子类化 UITableViewCell,

@interface RoundingCell : UITableViewCell

@property (nonatomic,weak) IBOutlet UILabel * someLabel;

@end

@implementation RoundingCell

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.someLabel.layer.cornerRadius = CGRectGetHeight(self.someLabel.bounds)/2;
    self.someLabel.layer.masksToBounds = YES;
}

@end

并将其用作所需单元格的类,以及 IBOutlet 连接。

【讨论】:

    【解决方案3】:

    很奇怪,我只是用你的代码来制作我的圆形单元格。 (而且我也使用自动布局)。

    唯一的区别是,我使用 .frame 而不是 .bounds(除以 2)。你试过吗?

    否则,您可以使用自定义单元格并在 -awakeFromNib 设置中以相同的方式进行舍入,因此您不必在每个单元格中都这样做。

    【讨论】:

    • 我确实有这样的结构:contentView->UIView->UILabel 所以也许我需要子类化 UIView 并在 awakeFromNib 或 layoutSubViews 中使 UILabel 圆在那里?
    • 如果您在视图中有标签,那么您必须拥有可以使用的IBOutlets。像cell.myView.layer.frame.width/2 这样的东西应该可以完成这项工作。当然不要忘记面具。
    【解决方案4】:

    我最终做了一个名为RoundView的课程

    class RoundView:UIView {
        override func layoutSubviews() {
            super.layoutSubviews()
    
            self.layer.cornerRadius = self.bounds.width/2
            self.layer.masksToBounds = true
        }
    }
    

    然后我将它应用到我需要圆的每个视图。所以在情节提要中,我将RoundView 添加到Custom Class

    发生的情况是,如果您查看情节提要 (XML) 的源代码,每个视图都有整个屏幕的大小,您可以查看自己的 SB 代码。因此,通过尝试在其父级layoutSubviews() 内添加等于width/2 的角半径,该子视图的框架设置不正确。所以圆角半径的值是320/2而不是50/2,这就是它变形的原因。

    【讨论】:

      【解决方案5】:

      1.创建UIView/category的自定义类

      #import <UIKit/UIKit.h>
      @interface RoundView : UIView
      
      @end
      #import "RoundView.h"
      

      2.添加layoutSubviews方法并设置圆角半径。

      @implementation RoundView
      
      -(void)layoutSubviews{
      
      [super layoutSubviews];
      
      self.layer.cornerRadius = self.bounds.size.width/2;
      
      self.layer.masksToBounds = true;
      
      }
      

      3.将你的 UIView 设为 RoundView 的子类并运行应用程序,你可以看到圆形视图。

      【讨论】:

        【解决方案6】:

        我的 imageview 也有同样的问题,所以我通过子类化 UIImageView 解决了它。

        @interface MyImageView : UIImageView
        
        @end
        

        @implementation MyImageView
        
        -(void)layoutSubviews
        {
            [super layoutSubviews];    
            self.layer.cornerRadius = self.bounds.size.width / 2.0;
        }
        @end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-11-28
          • 1970-01-01
          • 2021-03-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多