【问题标题】:How to set square edges for UITextField in iOS如何在 iOS 中为 UITextField 设置方形边缘
【发布时间】:2014-03-20 12:43:33
【问题描述】:

我正在尝试在我的应用上创建一个登录页面,其文本字段类似于此图中的电子邮件和密码字段:

我想要圆顶和方形底。我知道如何使用石英等创建完全圆形的边缘,但我不确定如何做一半和一半,我发现了一些关于类似问题的其他帖子,建议使用类似 myTextField.borderStyle = UITextBorderStyleRoundedRect; 的东西,但显然这确实完全四舍五入而不是一半.

【问题讨论】:

  • 更多信息请参考此链接:stackoverflow.com/questions/19910361/…
  • 你可以使用旧的脏方法和分段表视图
  • UIView 有两个UITextField 对象作为子视图,UIView 的图层有圆角,子视图被剪裁。如果有人想像 asnwer 那样锻炼,那就去做吧,但想法就是这样。

标签: ios cocoa-touch xamarin


【解决方案1】:

是的,您可以通过设置 textField 的背景图片来做到这一点。它可以在故事板中完成。

【讨论】:

    【解决方案2】:

    我认为最简单的方法是创建一个UIView 并将两个字段都放在该视图中。然后,您可以将视图设置为圆角。它将完全实现您想要的。

    【讨论】:

      【解决方案3】:

      可能比@George 和@Holex 的建议更复杂,但你可以这样做:

      TopTextField:

      UIBezierPath *topMaskPath = [UIBezierPath bezierPathWithRoundedRect:topTextField.bounds 
                                                        byRoundingCorners:UIRectCornerTopLeft| UIRectCornerTopRight                                                       
                                                              cornerRadii:CGSizeMake(10.0, 10.0)];
      // Create the shape layer and set its path
      CAShapeLayer *topMaskLayer = [CAShapeLayer layer];
      topMaskLayer.frame = topTextField.bounds;
      topMaskLayer.path = topMaskPath.CGPath;
      // Set the newly created shape layer as the mask for the image view's layer
      topTextField.layer.mask = topMaskLayer;
      

      底部文本字段:

      UIBezierPath *bottomMaskPath = [UIBezierPath bezierPathWithRoundedRect:topTextField.bounds 
                                                           byRoundingCorners:UIRectCornerBottomLeft| UIRectCornerBottomRight                                                       
                                                                 cornerRadii:CGSizeMake(10.0, 10.0)];
      // Create the shape layer and set its path
      CAShapeLayer *bottomMaskLayer = [CAShapeLayer layer];
      bottomMaskLayer.frame = bottomTextField.bounds;
      bottomMaskLayer.path = bottomMaskPath.CGPath;
      // Set the newly created shape layer as the mask for the image view's layer
      bottomTextField.layer.mask = bottomMaskLayer;
      

      基于:here

      【讨论】:

        【解决方案4】:

        尝试为文本字段和按钮使用背景图片。它唯一最简单的方法。我只为我的所有应用程序使用图像..

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-14
          • 1970-01-01
          • 2011-03-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多