Browse Source

iOS Sidebar menu: fix layout within the iPad's split view feature

Felix Paul Kühne 9 years ago
parent
commit
2cc33b4311
3 changed files with 53 additions and 10 deletions
  1. 41 10
      Sources/VLCMenuTableViewController.m
  2. 1 0
      Sources/VLCSidebarController.h
  3. 11 0
      Sources/VLCSidebarController.m

+ 41 - 10
Sources/VLCMenuTableViewController.m

@@ -34,6 +34,8 @@
 #define IPAD_ROW_HEIGHT 65.
 #define HEADER_HEIGHT 22.
 #define MENU_WIDTH 320.
+#define MAX_LEFT_INSET 170.
+#define COMPACT_INSET 20.
 
 static NSString *CellIdentifier = @"VLCMenuCell";
 static NSString *WiFiCellIdentifier = @"VLCMenuWiFiCell";
@@ -47,6 +49,9 @@ static NSString *WiFiCellIdentifier = @"VLCMenuWiFiCell";
     NSMutableSet *_hiddenSettingKeys;
 
     UITableView *_menuTableView;
+
+    NSArray <NSLayoutConstraint *> *_leftTableConstraints;
+    CGFloat _tableViewWidth;
 }
 @property (strong, nonatomic) IASKAppSettingsViewController *settingsViewController;
 @property (strong, nonatomic) VLCSettingsController *settingsController;
@@ -78,21 +83,21 @@ static NSString *WiFiCellIdentifier = @"VLCMenuWiFiCell";
         rowHeight = ROW_HEIGHT;
     CGFloat height = (count * rowHeight) + (3. * HEADER_HEIGHT);
     CGFloat top;
-    if (height > screenDimensions.size.height - 20.) {
-        height = screenDimensions.size.height - 20.;
-        top = 20.;
+    if (height > screenDimensions.size.height - COMPACT_INSET) {
+        height = screenDimensions.size.height - COMPACT_INSET;
+        top = COMPACT_INSET;
     } else
         top = (screenDimensions.size.height - height) / 2.;
     CGFloat left;
     if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
-        left = 170.;
+        left = MAX_LEFT_INSET;
     else
-        left = 20.;
-    CGFloat width = MENU_WIDTH;
-    if (screenDimensions.size.width <= left + width)
-        width = width - (left * 2.);
+        left = COMPACT_INSET;
+    _tableViewWidth = MENU_WIDTH;
+    if (screenDimensions.size.width <= left + _tableViewWidth)
+        _tableViewWidth = _tableViewWidth - (left * 2.);
 
-    _menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(left, top, width, height)
+    _menuTableView = [[UITableView alloc] initWithFrame:CGRectMake(left, top, _tableViewWidth, height)
                                                           style:UITableViewStylePlain];
     _menuTableView.delegate = self;
     _menuTableView.dataSource = self;
@@ -124,7 +129,8 @@ static NSString *WiFiCellIdentifier = @"VLCMenuWiFiCell";
     }
 
     dict = NSDictionaryOfVariableBindings(_menuTableView);
-    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-==%0.2f-[_menuTableView(%0.2f)]->=0-|", left, width] options:0 metrics:0 views:dict]];
+    _leftTableConstraints = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-==%0.2f-[_menuTableView(%0.2f)]->=0-|", left, _tableViewWidth] options:0 metrics:0 views:dict];
+    [self.view addConstraints:_leftTableConstraints];
 
     [_menuTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
 }
@@ -140,6 +146,31 @@ static NSString *WiFiCellIdentifier = @"VLCMenuWiFiCell";
     return YES;
 }
 
+- (void)viewWillLayoutSubviews
+{
+    CGFloat viewWidth = self.view.frame.size.width;
+
+    [self.view removeConstraints:_leftTableConstraints];
+
+    CGFloat left;
+    if (viewWidth >= _tableViewWidth) {
+        left = (viewWidth - _tableViewWidth) / 3.;
+        if (left > MAX_LEFT_INSET)
+            left = MAX_LEFT_INSET;
+    } else {
+        _tableViewWidth = MENU_WIDTH;
+        left = 0.;
+    }
+
+    NSDictionary *dict = NSDictionaryOfVariableBindings(_menuTableView);
+    _leftTableConstraints = [NSLayoutConstraint constraintsWithVisualFormat:[NSString stringWithFormat:@"H:|-==%0.2f-[_menuTableView(%0.2f)]->=0-|", left, _tableViewWidth] options:0 metrics:0 views:dict];
+    [self.view addConstraints:_leftTableConstraints];
+
+    [super viewWillLayoutSubviews];
+
+    [[VLCSidebarController sharedInstance] resizeContentView];
+}
+
 #pragma mark - table view data source
 
 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

+ 1 - 0
Sources/VLCSidebarController.h

@@ -18,6 +18,7 @@
 
 - (void)hideSidebar;
 - (void)toggleSidebar;
+- (void)resizeContentView;
 
 - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath scrollPosition:(UITableViewScrollPosition)scrollPosition;
 

+ 11 - 0
Sources/VLCSidebarController.m

@@ -20,6 +20,7 @@
     RESideMenu *_sideMenuViewController;
     VLCMenuTableViewController *_menuViewController;
     UIViewController *_contentViewController;
+    BOOL _menuVisible;
 }
 
 @end
@@ -88,11 +89,13 @@
 
 - (void)hideSidebar
 {
+    _menuVisible = NO;
     [_sideMenuViewController hideMenuViewController];
 }
 
 - (void)toggleSidebar
 {
+    _menuVisible = YES;
     if ([UIApplication sharedApplication].userInterfaceLayoutDirection == UIUserInterfaceLayoutDirectionLeftToRight) {
             [_sideMenuViewController presentLeftMenuViewController];
     } else {
@@ -100,4 +103,12 @@
     }
 }
 
+- (void)resizeContentView
+{
+    if (_menuVisible) {
+        [self hideSidebar];
+        [self toggleSidebar];
+    }
+}
+
 @end