iOS-更改子类UIView的子视图的框架(使用启用自动布局的情节提要) [英] iOS - Changing frame of a subview of a subclassed UIView (using storyboard with auto layout enabled)

查看:226
本文介绍了iOS-更改子类UIView的子视图的框架(使用启用自动布局的情节提要)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个自定义进度栏(UIView的子类). 我将UIImageView添加到使用重复图像显示进度的UIView中. 该视图已添加到情节提要中,我可以使其显示得很好,但是,如果我尝试更改框架,则仅显示情节提要文件中显示的内容 (即,如果情节提要视图的背景为黄色,但uiview子类的代码将其更改为绿色,如果我尝试更改代码中的UIImageView的框架以反映当前进度,则默认为黄色背景)

I am building a custom progress bar (subclass of UIView). I add an UIImageView to the UIView that uses repeating images to display the progress. The view is added to the storyboard and I can get it to display just fine, however if I try to change the frame then it only displays whatever is shown in the storyboard file (i.e. if the storyboard view has a yellow background, but the code of the uiview subclass changes it to green, if I try to change the frame of the UIImageView in the code to reflect the current progress then it defaults back to the yellow background)

我没有运气就尝试使用它们:

I tried using these with no luck:

[self updateConstraints];
[self layoutSubviews];

更新

我知道我需要为自动布局设置约束.但是,在我以这种方式创建子类UIView时,需要以编程方式完成它.这是我正在尝试的代码,我知道我已经接近了,但是不能完全正常工作:

I know I need to setup a constraint for use with autolayout. However, it needs to be done programmatically as I am creating the subclassed UIView this way. Here is the code I am trying, I know I am close but can't quite get it to work:

[self addConstraint:
 [NSLayoutConstraint constraintWithItem:self.progressView
                              attribute:NSLayoutAttributeWidth
                              relatedBy:NSLayoutRelationLessThanOrEqual
                                 toItem:nil
                              attribute:NSLayoutAttributeWidth
                             multiplier:1
                               constant:progressWidth]];

[UIView animateWithDuration:3 animations:^{
    [self.progressView layoutIfNeeded];
}];

推荐答案

使用自动布局时,切勿触摸框架.使用这样的布局系统的全部要点是,框架是基于附加在框架上的可满足约束来推断的.

When working with autolayout one should never touch the frame. The whole point of using a layout system like this is that frames are inferred based on the satisfiable constraints attached to them.

不知道更多详细信息的方法是从故事板到相关视图创建NSLayoutConstraint出口.例如,您可能在进度视图上具有宽度限制-创建 NSLayoutConstraint 到宽度约束的出口将允许您更新constant,这反过来将调整框架.

Without knowing some more details the way to approach this would be to create NSLayoutConstraint outlets from your storyboard to the view in question. For example you might have a width constraint on the progress view - creating an NSLayoutConstraint outlet to the width constraint will allow you to update the constant, which in turn will adjust the frame.

动画示例:

self.widthConstraint.constant = 300;

[UIView animateWithDuration:0.4 animations:^{
    [self.view layoutIfNeeded];
}];

更新5/15/2013

这里是使用可视化格式的约束(易于使用)以编程方式创建约束的示例.

Here is an example of creating the constraint programatically using the visual format for constraints (easier to use)

UIView *w_progressView = self.progressView
NSArray *wc = [NSLayoutConstraint constraintsWithVisualFormat:@"[w_progressView(200)]"
                                                                              options:0
                                                                              metrics:nil
                                                                                views:NSDictionaryOfVariableBindings(w_progressView)];

(很抱歉格式化,它是一个很长的方法...)

(sorry for the formatting, its a long method...)

然后将返回的约束 NSArray 添加到父视图:

Then add the NSArray of constraints returned to the parent view:

[self addConstraints:wc];

符号progressView(200)在视图 progressView

这篇关于iOS-更改子类UIView的子视图的框架(使用启用自动布局的情节提要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆