我想在我的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方法內調用initWithDictionary或任何我最終使用?我已經看到它與layoutSubViews等完成,我應該在實際的子視圖類中使用什麼方法/方法? – StuartM
在你的initWithDictionary中,你會做self = [super init],它使你的對象變成一個UIView。在此之後,做任何你需要做的事情來使它成爲你的。當然,添加子視圖,然後將它們放在layoutSubviews中,或使用自動佈局,這看起來像你最初想做的。 – jsd
嘿@jsd。這裏的實際要求是從問題中製作'bottomBarInnerView'。該字典將不再需要,因爲該字典用於該視圖上的約束。我需要繼承'UIView'來創建'bottomBarInnerView'。所以字符串,計數等將被傳遞到相關的新的init方法和代碼[AFTER]上面的大塊。無論如何,問題是我如何繼承UIView並正確配置它。即我子類'UIView',然後調用alloc/init - 然後把所有的代碼之後。或者我創建一個自定義的'initWithString'方法,然後調用... – StuartM