ECSlidingViewController crashes on second swipe - ecslidingviewcontroller

I'm building a new project using ECSlidingViewController v2. The first time I swipe to open the left under view, it works great. But when I swipe the second time, I get an EXC_BAD_ACCESS crash. The reference is to the NSMapTable in the controller.
- (void)updateTopViewGestures {
BOOL topViewIsAnchored = self.currentTopViewPosition == ECSlidingViewControllerTopViewPositionAnchoredLeft ||
self.currentTopViewPosition == ECSlidingViewControllerTopViewPositionAnchoredRight;
UIView *topView = self.topViewController.view;
if (topViewIsAnchored) {
...
} else {
...
---> [self.customAnchoredGesturesViewMap removeAllObjects];
}
}

Taking reference from #uiroshan answer Closing ECSlidingViewController menu
You can use the following code:
- (IBAction)showSlidingMenu:(id)sender
{
[self.slidingViewController anchorTopViewToRightAnimated:YES];
if ([self.slidingViewController currentTopViewPosition] == ECSlidingViewControllerTopViewPositionAnchoredRight)
{
[self.slidingViewController resetTopViewAnimated:YES];
}
}
Here,
I am animating the top view controller to Right, so I am checking whether top view position is ECSlidingViewControllerTopViewPositionAnchoredRight
To animating the top view controller to Left
ECSlidingViewControllerTopViewPositionAnchoredLeft

Related

UISearchController background not matching UINavigationBar background in iOS 11

I'm updating an older app to support iPhone X and when updating a searchable UITableViewController the background of the UISearchController doesn't change when placing it in the navigation bar.
I'm using the following code to place it in the navigation bar:
self.navigationItem.searchController = self.searchController;
self.navigationItem.hidesSearchBarWhenScrolling = NO;
Inspecting the Interface shows the following hierarchy:
_UIBarBackground contained within the _UINavigationControllerManagedSearchPalette remains white.
Is there something I've missed when creating the UINavigationController?
Try to apply background color same as the navigationBar tint color.
searchBar.tintColor = UIColor.white
searchBar.backgroundColor = UIColor.red
searchBar.clearBackgroundColor()
Remove color of the searchBar by this way:
extension UISearchBar {
func clearBackgroundColor() {
guard let UISearchBarBackground: AnyClass = NSClassFromString("UISearchBarBackground") else { return }
for view in self.subviews {
for subview in view.subviews {
if subview.isKind(of: UISearchBarBackground) {
subview.alpha = 0
}
}
}
}
}
with above code I am able to achieve this output
Hope this help.

Swipe Between Two UIViews

How can I swipe between two UIViews using Pan Gesture Handler with Swift?
I would like to be able to swipe left and begin to reveal the next UIView, but snap back if the gesture isn't complete (to a certain point).
Currently using Swipe Gesture (but it doesn't animate and show preview halfway through)
func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.Right:
println("Swiped right")
loadView1()
case UISwipeGestureRecognizerDirection.Left:
println("Swiped left")
loadView2()
default:
break
}
}
}
How can I make the gesture start revealing the next UIView and snap back if the gesture isn't complete?
You should check the start and end position of your swipe and load the different views if the distance isn't long enough:
var startLocation:CGPoint!
var endLocation:CGPoint!
if(gesture.state == .Began){
startLocation = gesture.locationInView(self.view)
} else if(gesture.state == .Ended){
endLocation = gesture.locationInView(self.view)
var distance = endLocation.x - startLocation.x
}

UIScrollView only scrolling to left by clicking UIButton

In my app I have a scrollView. It should only scroll to the right when a UIButton is clicked. Scrolling to the left should always be possible. I found a solution like this:
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
if (scrollView.contentOffset.x < oldX) {
[aScrollView setContentOffset: CGPointMake(oldX, aScrollView.contentOffset.y)];
} else {
oldX = aScrollView.contentOffset.x;
}
}
This code stops it from scrolling to the left, but i can't make this work for the right side. Any ideas how this could be done?
Thanks

Detecting UIScrollView Position

I am trying to use a UIScrollView as a navigation device in my iOS application. Basically, I want the application to load specific content, based on the position of a paging-enabled UIScrollView. It is sort of like a navigation wheel.
Currently, I have a two-page (2*320) scrollview, and I am using the scrollViewDidEndDragging delegate method and contentoffset.x to load the appropriate content. I try to determine the position of the scrollview as such:
if (scrollView.contentOffset.x < 320) {
// Load content 1
}
else if (scrollView.contentOffset.x >= 320) {
// Load content 2
}
My problem is that I either do not understand how to work with contentoffset or it is not suitable to deal with my problem. Either way, I am stuck. Can someone let me know where I went wrong and/or show me a more efficient way to determine scrollview position?
I am stupid, I forgot to mention what the problem is... Basically, I cannot track the position of the scrollview. When I move to the second page it only registers changes if I go over the 620 limit (bounce rightward). But when I go back to the starting position (page 1), it does register the changes. In other words, my tracking is not accurate.
Delegate and everything else is working fine, I log them!
It's hard to say because you never specifically mentioned what the problem is, but I'll take a couple guesses at it.
If the delegate method isn't being called at all you need to remember to set the scroll views delegate:
[myScrollView setDelegate:self];
Otherwise, the problem with using scrollViewDidEndDragging to detect the scroll views offset is that it will check the offset at the point where you stop dragging which could be within the destination pages rect, or within the starting pages rect. As an alternative, I'd suggest you use scrollViewDidEndDecelerating to check the content offset as it will be called as soon as the scroll view comes to a stop at its destination page.
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
if (scrollView.contentOffset.x < 320) {
NSLog(#"%#",NSStringFromCGPoint(scrollView.contentOffset));
}
else if (scrollView.contentOffset.x >= 320) {
NSLog(#"%#",NSStringFromCGPoint(scrollView.contentOffset));
}
}
I think this could help you :). With scrollViewDidScroll you always get the exact position of your scrollView:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.x < 320)
{
// Load content 1
}
else if (scrollView.contentOffset.x >= 320)
{
// Load content 2
}
}
NSLog(#"Position: %g", scrollView.contentOffset.x);
Do not forget the delegate UIScrollViewDelegate in your .h file.
Here a solution for getting the current position (page) as integer Value:
Use scrollViewDidEndDecelerating: methode:
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
NSLog(#"%i", (int)(_scrollView.contentOffset.x / _scrollView.frame.size.width));
}
If for any reason, you'd like to be notified before the view ended decelerating; for example, to perform animations. You can also use this:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Where the scroll will end (vertically)
let offSetY = targetContentOffset.pointee.y
// I've got a scroll view with paging enabled so I use this to check which "page" is selected.
if offSetY == firstPageView.frame.origin.y {
// Do something…
} else if offSetY == secondPageView.frame.origin.y {
// Do something…
}
}
Using UIScrollViewDelegate,
func scrollViewWillBeginDragging(scrollView: UIScrollView){
print("dragging begins")
print("Position: \(scrollView.contentOffset.x) , \(scrollView.contentOffset.y) ")
}

Calculating left UIBarButtonItem width

I found out the hard way UIBarButtonItem frame width is 0. After googling, i found a method to traverse the nav controllers subviews to find the left bar button's width. It works but it just seems like there has to be a cleaner way to do this.
There would be no reason my app would get rejected for using this method right?
UIView *leftBarButtonView = nil;
for (UIView* v in self.navigationController.navigationBar.subviews) {
if ([[v class].description isEqualToString:#"UINavigationButton"]) {
if (leftBarButtonView==nil) {
if (v.frame.origin.x > 0.0)
leftBarButtonView = v;
} else if (v.frame.origin.x < leftBarButtonView.frame.origin.x && v.frame.origin.x>0.0) {
leftBarButtonView = v; // this view is further right
}
}
}
No, it won't probably get rejected - probably, as nobody except Apple can say it for sure. However, one thing is sure:
[[v class].description isEqualToString:#"UINavigationButton"]
won't work. Use
[v isKindOfClass:objc_getClass("UINavigationButton")]
instead.