【发布时间】:2014-02-14 17:35:18
【问题描述】:
我正在尝试在我的UIViewController 中正确重构一个方法。我对 Objective-C 相当陌生,因此试图了解这里最好的方法是什么。方法是在屏幕上创建一个自定义底栏。它是一个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,那么这个视图是否应该被传递到创建横幅栏子视图的新方法中??
【问题讨论】:
标签: ios objective-c uiview ios7 uiviewcontroller