重构在UIViewController中创建自定义UIView的大型方法
问题描述:
我想在我的UIViewController
中正确地重构方法。我对Objective-C相当陌生,所以试图理解这里最好的方法。该方法是在屏幕上创建一个自定义底部栏。这是一个UIView
,并有多个子视图和自动布局。重构在UIViewController中创建自定义UIView的大型方法
目前酒吧的整个创作都是采用一种方法,约300行。理想情况下,我想将其移入子类,但需要了解如何正确完成此操作。
下面是该方法的开始感受一下它的作用:
-(void)setupBottomBarViewForOrientation:(UIInterfaceOrientation)forOrientation {
UIView *bottomBarInnerView = [UIView autolayoutView];
[self.bottomBarView addSubview:bottomBarInnerView];
NSDictionary *views = NSDictionaryOfVariableBindings(bottomBarInnerView);
NSDictionary *bottomBarMetrics = @{@"minVPadding":@2.0};
[self.bottomBarView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(minVPadding)-[bottomBarInnerView]-(minVPadding)-|" options:0 metrics:bottomBarMetrics views:views]];
[self.bottomBarView addConstraint:[NSLayoutConstraint constraintWithItem:bottomBarInnerView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.bottomBarView attribute:NSLayoutAttributeCenterX multiplier:1 constant:1]];
NSString *word = @"Word";
NSUInteger letterCount = [word length];
NSMutableArray *viewList = [[NSMutableArray alloc] init]; // Create an array to store the tiles within
NSDictionary *metrics = @{@"letterTileMinHeight":@40.0,@"padding":@3.0,@"letterViewPadding":@5.0}; // Constraint metrics
... [OTHER LOGIC] which create multiple subviews further UIViews and UILabels ...
是将它变成UIView
子类的最佳途径?那么定制代码在哪里(哪个方法)?另外它是如何从VC调用并创建自动布局的?
我明白子类等,主要问题是如何创建自动布局约束。一些后来的自动布局约束从UIViewController
链接到self.view,所以这个视图应该传递到创建横幅栏子视图的新方法中?
答
你应该绝对让它成为一个UIView子类。通过你需要的任何配置。看起来像一本字典,现在,所以你可以打电话给你的init()方法类似
-(MyNewView*)initWithDictionary:(NSDictionary*)dict {}
视图子类将设立自己的约束条件是它里面的东西。如果使用它的视图控制器在整个子视图上需要约束,那么它应该添加它们,但不应该混淆它。
下面是一个子视图init方法的示例,该方法需要字符串& count,创建两个标签以显示它们,并将它们添加到自身。您可以布置子视图或自动布局以将它们放置在新视图中。
-(BottomBarInnerView *)initWithString:(NSString *)string count:(NSNumber*)count {
self = [super initWithFrame:CGRectZero];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
label.text = string;
[self addSubview:label];
UILabel *countLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 20, 10)];
countLabel.text = [count stringValue];
[self addSubview:countLabel];
return self;
}
由于视图使用CGRectZero你需要在创建它来调整其大小,所以它会是这样的:
BottomBarInnerView* bottomBarInnerView = [[BottomBarInnerView alloc] initWithString:@"Hi Mom" count:@(42)];
bottomBarInnerView.frame = CGRectMake(0, 200, 100, 50);
[self.view addSubview:bottomBarInnerView];
另外,您可以添加约束它,并让自动布局集为你的框架。
好吧,我创建'UIView'的子视图然后在init方法内调用initWithDictionary或任何我最终使用?我已经看到它与layoutSubViews等完成,我应该在实际的子视图类中使用什么方法/方法? – StuartM
在你的initWithDictionary中,你会做self = [super init],它使你的对象变成一个UIView。在此之后,做任何你需要做的事情来使它成为你的。当然,添加子视图,然后将它们放在layoutSubviews中,或使用自动布局,这看起来像你最初想做的。 – jsd
嘿@jsd。这里的实际要求是从问题中制作'bottomBarInnerView'。该字典将不再需要,因为该字典用于该视图上的约束。我需要继承'UIView'来创建'bottomBarInnerView'。所以字符串,计数等将被传递到相关的新的init方法和代码[AFTER]上面的大块。无论如何,问题是我如何继承UIView并正确配置它。即我子类'UIView',然后调用alloc/init - 然后把所有的代码之后。或者我创建一个自定义的'initWithString'方法,然后调用... – StuartM