在CALayer的IOS自动布局约束

在CALayer的IOS自动布局约束

本文介绍了在CALayer的IOS自动布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在开发中,我试图设置一个侧面边框的EditText iPhone应用程序。我这样做的方式如下:

Hi I am developing iPhone application in which I tried to set one side border for edittext. I did this in following way:

 int borderWidth = 1;
CALayer *leftBorder = [CALayer layer];

leftBorder.borderColor = [UIColor whiteColor].CGColor;
leftBorder.borderWidth = borderWidth;

leftBorder.frame = CGRectMake(0, textField.frame.size.height - borderWidth, textField
                              .frame.size.width, borderWidth);
[textField.layer addSublayer:leftBorder];

我把一些制约我在IB的EditText这样,当我转动我的设备,它会根据该调整文本框的宽度。我的问题是调整的不的EditText调整的CALayer的宽度,我为我的编辑文本设置宽度。所以我觉得我必须把一些约束我的CALayer项目也是如此。但我不知道该怎么做。任何人知道这件事吗?需要帮助。谢谢你。

I put some constraints on my edittext in IB so that when I rotate my device it will adjust width of text field according to that. My problem is that adjusts the width of edittext not adjusting the width of CALayer which I set for my edit text. So I think I have to put some constraints for my CALayer item as well. But I dont know how to do that. ANy one knows about this? Need Help. Thank you.

推荐答案

整个企业自动尺寸是针对特定视图。层不会自动调整。

the whole autoresizing business is view-specific. layers don't autoresize.

您必须做的事情 - 在code - 是要调整自己的层

what you have to do -- in code -- is to resize the layer yourself

例如。

在一个的viewController你会做

in a viewController you would do

- (void) viewDidLayoutSubviews {
  [super viewDidLayoutSubviews]; //if you want superclass's behaviour...
  // resize your layers based on the view's new frame
  self.editViewBorderLayer.frame = self.editView.bounds;
}

或自定义的UIView你可以使用

or in a custom UIView you could use

- (void)layoutSubviews {
  [super layoutSubviews]; //if you want superclass's behaviour...  (and lay outing of children)
  // resize your layers based on the view's new frame
  layer.frame = self.bounds;
}

这篇关于在CALayer的IOS自动布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 04:09