Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Navigation Controller Problem #62

Closed
masabusharif opened this issue Sep 16, 2014 · 9 comments
Closed

Navigation Controller Problem #62

masabusharif opened this issue Sep 16, 2014 · 9 comments

Comments

@masabusharif
Copy link

I am using the segmented control with images and a scroll view (segmented control 4 in the demo) and everything is working perfectly except when i use a navigation controller, the area of the segmented control seem to have bigger content are and i can scroll vertically and move around the segmented control items

@HeshamMegid
Copy link
Owner

Hi Mohammad, could you please give me more info about how to reproduce this or add some code?

@masabusharif
Copy link
Author

The easiest way to reproduce this is to comment out all the segmented controllers in the Demo except segmentedControl4 and embed a navigation controller in the App delegate

self.viewController = [[ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navController;

If u uncomment the creation of segmented controller 3, everything will work fine, i am keeping the code and hiding the segmented controller 3 that i dont need, but i think there should be a more logical solution, i've been trying for 2 days. maybe you can point out the problem

Thanks

@ihenk
Copy link

ihenk commented Oct 17, 2014

I have the same problem, how can I do?Thanks!@masabusharif @HeshamMegid

@ihenk
Copy link

ihenk commented Oct 17, 2014

ios simulator screen shot 2014 10 18 12 05 02
ios simulator screen shot 2014 10 18 12 05 08
This UISegmentedControl in a UIView, but you can scroll up and down. During scrolling, his frame has remained unchanged.

@masabusharif
Copy link
Author

ihenk Can you provide some code in order to help you out?

@ihenk
Copy link

ihenk commented Oct 18, 2014

   _segmentedControl = [[HMSegmentedControl alloc] init];
    _segmentedControl.frame = CGRectMake(0, 0, self.width, NAVIGATION_BAR_HEIGHT);
    _segmentedControl.font = FONT_15;
    _segmentedControl.selectedFont = FONT_BOLD_15;
    _segmentedControl.backgroundColor = [UIColor clearColor];
    _segmentedControl.backgroundImage = [IMG(IC_SEGMENTED_N) resizableImageWithCapInsets:UIEdgeInsetsZero resizingMode:UIImageResizingModeStretch];
    _segmentedControl.selectionIndicatorColor = SEGMENT_BLUE_COLOR;
    _segmentedControl.selectionIndicatorLocation = HMSegmentedControlSelectionIndicatorLocationDown;
    _segmentedControl.selectionIndicatorHeight = 2;
    _segmentedControl.selectedTextColor = SEGMENT_BLUE_COLOR;
    _segmentedControl.textColor = [UIColor darkGrayColor];
    _segmentedControl.selectionStyle = HMSegmentedControlSelectionStyleTextWidthStripe;
    _segmentedControl.segmentWidthStyle = HMSegmentedControlSegmentWidthStyleDynamic;
    NSMutableArray *sectionTitles = [NSMutableArray array];
    for (int i = 0; i < _tableViews.count; i++) {
        HKAbstractTableView *tableView = _tableViews[i];
        tableView.pageIndex = i;
        [sectionTitles addObject:[tableView pageTitle]];
        tableView.frame = CGRectMake(i * _scrollView.frame.size.width, 0, _scrollView.frame.size.width, _scrollView.frame.size.height);
        [_scrollView addSubview:tableView];
    }
    [_segmentedControl setSectionTitles:sectionTitles];
    _segmentedControl.selectedSegmentIndex = self.currentPageIndex;
    [_segmentedControl addTarget:self action:@selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_segmentedControl];
    [self addSubview:_scrollView];

@ihenk
Copy link

ihenk commented Oct 18, 2014

 CampusNewsController *controller = [[CampusNewsController alloc] init];
 self.window.rootViewController = controller;
 [self.window makeKeyAndVisible];

Everything is working perfectly

UINavigationController *rooterViewController = [[UINavigationController alloc] initWithRootViewController:controller];
self.window.rootViewController = rooterViewController;
[self.window makeKeyAndVisible];

It does not work

@ihenk
Copy link

ihenk commented Oct 19, 2014

notes:My English is not good, if the English grammar wrong, please forgive me.

I found the cause of the problem, because the scrollView (defined in HMSegmentedControl) of contentOffset changed.I found the cause of the problem, because the scrollView (defined in HMSegmentedControl) of contentOffset(y changed) changed. Now, I'm using KVO listening scrollView of contentOffset, when it changes, I reset contentOffset (self.scrollView.contentOffset.x,0).

I modified the HMSegmentedControl.m file.
step one:
add this code in commonInit method.

[self.scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:NULL];

step two:
add this code in HMSegmentedControl.m.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"contentOffset"]) {
        if (self.scrollView.contentOffset.y != 0) {
            self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
        }
    }
}

Now everything works perfectly, but I do not know why scrollView of contentOffset(y change) will change.

@HeshamMegid
Copy link
Owner

This happens because of the view controller automatically adjusting the content insets of the scroll view.

There are 2 ways around this:

  1. Add self.automaticallyAdjustsScrollViewInsets = NO; to the view controller that adds the segmented control.
  2. Add self.scrollView.contentInset = UIEdgeInsetsZero; to updateSegmentsRects.

I'll commit the fix but I won't do a Cocoapods release. So you could either point your pod to the head of master (recommended) or do the fix manually.

HeshamMegid pushed a commit that referenced this issue Oct 19, 2014
hons82 added a commit to hons82/HMSegmentedControl that referenced this issue Nov 10, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants