CALayer IOS上的自动布局约束

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

问题描述

您好,我正在开发 iPhone 应用程序,我在其中尝试为 edittext 设置一侧边框.我是通过以下方式做到的:

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.

你必须做的——在代码中——是自己调整层的大小

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上的自动布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Get an event when UIBarButtonItem menu is displayed(显示UIBarButtonItem菜单时获取事件)