【问题标题】:How to change the frame of a view when a variable value changes?当变量值改变时如何改变视图的框架?
【发布时间】:2013-05-20 05:37:09
【问题描述】:

在我的视图控制器中,视图很少。该视图的所有框架都取决于变量

CGFloat 边框宽度

这些视图的定义如下

sec1 = [[MSSectionView alloc]initWithFrame:CGRectMake(self.borderWidth, self.borderWidth,self.frame.size.width/2-(self.borderWidth*3/2),self.frame.size.height/2 - (self.borderWidth*3/2) ) ];

当我从另一个类更改borderwidth 的值时,我想更改sec1 的框架。 我们怎么能这样做? 我知道

[sec1 setFrame:CGRectMake(self.borderWidth, self.borderWidth,self.frame.size.width/2-(self.borderWidth*3/2),self.frame.size.height/2 - (self.borderWidth*3/2) )];

会改变框架,但有很多 uiviews。所以我不能用这种方法为所有这些设置框架。

【问题讨论】:

  • 所有子视图的框架都以同样的方式计算?

标签: ios objective-c deep-copy shallow-copy cgrectmake


【解决方案1】:

你可以这样实现:

在 MSSectionView.m 中:

#import "MSSectionView.h"

@implementation MSSectionView

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString: @"borderWidth"])
    {
        CGFloat borderWidth = [(NSNumber*)[change objectForKey:NSKeyValueChangeNewKey] floatValue];
        [self setFrame: CGRectMake(borderWidth, borderWidth, self.frame.size.width/2 - (borderWidth * 3/2), self.frame.size.height/2 - (borderWidth * 3/2))];
    }
    else
    {
        [super observeValueForKeyPath: keyPath
                             ofObject: object
                               change: change
                              context: context];
    }
}

@end

在 viewcontroller 中,它拥有一些 MSSectionView 作为子视图:

@implementation TSViewController

@synthesize borderWidth;

- (void) viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSArray* views = [self.view subviews];

    for (UIView* subview in views)
    {
        if ([subview isKindOfClass: [MSSectionView class]])
        {
            MSSectionView* sectionView = (MSSectionView*) subview;
            [self addObserver: sectionView
                   forKeyPath: @"borderWidth"
                      options: NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                      context: NULL];
        }

    }
}

在 viewcontroller.h 中:

@interface TSViewController : UIViewController
{
    CGFloat borderWidth;
}

@property(nonatomic,readwrite,assign)CGFloat borderWidth;

【讨论】:

    【解决方案2】:

    为了实现这一点,需要一个 for 循环并更改所有帧。

    for(UIView *subview in yourview.subviews)
    {
        // Here you can change your frames based on the condition
        //here you will get old frame value of subview so you can change by using the old frame values of all the subviews.
        [subview setFrame:CGRectMake(self.borderWidth, self.borderWidth,self.frame.size.width/2-(self.borderWidth*3/2),self.frame.size.height/2 - (self.borderWidth*3/2) )];
    }
    

    【讨论】:

    • 我的所有部分都没有相同的框架。每个框架都不同。
    • 我知道它会起作用。如果只有一个uiview,那就很容易了。但我有大约 40 个不同框架的 uiview。所以我不能在这个方法中设置它们中的每一个
    【解决方案3】:

    您可以考虑使用 KVO。在你的sec1 类中观察borderWidth 的变化。这样,如果borderWidthe发生变化,你的sec1类也可以相应变化。

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        [self addObserver:self forKeyPath:@"borderWidth" options:NSKeyValueObservingOptionNew context:&self->borderWidth];
    }
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
    {
        if (context == &self.borderWidth) {
            if ([keyPath isEqualToString:@"borderWidth"]) {
                // Calculate your subview's frame.
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多