i have a Scroll View with paging enabled. and a long width so i can select between my 3 pages. But Every time that i click somewhere on the left or right side it goes back to the middle view. how can i fix it? here's a video sample http://www.youtube.com/watch?v=F1bj7_CBt-g
and this is the code in my viewDidLoad for that paging function.
pageIndex = 0;
self.myScrollView.contentInset = UIEdgeInsetsMake(0, 320, 0, 0);
self.myScrollView.pagingEnabled = YES;
self.myScrollView.clipsToBounds = NO;
[self.myScrollView setContentSize:CGSizeMake(1280, 120)];
thank you for all your help!
Instead of self.myScrollView.contentInset = UIEdgeInsetsMake(0, 320, 0, 0);
Try self.myScrollView.contentInset = UIEdgeInsetsZero;. I suspect it is the problem.
If you want to by default scroll to one page, you can use
[self.myScrollView scrollRectToVisible:([Your rect]) animated:NO];
Related
I am trying to create a header like the one in the App Store. Where when the user drags down, the header stays in place and only the cells scroll down. But when the user drags up, both the header and cells scroll up.
What I have done so far is set my collectionView background view to the image I want to stick to the top and set contentInsets of the collectionView below the image. This way when the user drags down, the image will stay in place. But when the user drags up, my cells go over the image (I want the image to scroll up with the cells). I think I am going about this wrong. Thanks.
UIView *back = [UIView new];
UIImageView *backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
backImage.image = [UIImage imageWithData:coverData];
[back addSubview:backImage];
self.collectionView.backgroundView = back;
[self.collectionView setContentInset:UIEdgeInsetsMake(300, 0, 0, 0)];
EDIT
I used the tutorial from this question but this does the opposite of what I want, and I am unsure of how to change that code to fit my needs
Ok I got it. If you are using the tutorial posted from the linked question-
Change
origin.y = MIN(
MAX(
contentOffset.y,
(CGRectGetMinY(firstCellAttrs.frame) - headerHeight)
),
(CGRectGetMaxY(lastCellAttrs.frame) - headerHeight)
);
To This
origin.y = MIN(
contentOffset.y,
(CGRectGetMinY(firstCellAttrs.frame) - headerHeight)
);
I have my app setup to show this view when it is loading data:
self.loadingView = [UIView new];
self.loadingView.frame = CGRectMake(0, 0, self.tableView.frame.size.width, self.view.frame.size.height);
self.loadingView.backgroundColor = [UIColor groupTableViewBackgroundColor];
[self.view addSubview:self.loadingView];
[self.view bringSubviewToFront:self.loadingView];
self.activityIndicator = [UIActivityIndicatorView new];
self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
self.activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
[self.view addSubview:self.activityIndicator];
[self.view bringSubviewToFront:self.activityIndicator];
[self.activityIndicator startAnimating];
Then, I remove it from its superview. It works on iPhone. It works on iPad sometimes too, except for when I'm using the same code in a UISplitViewController. I've tried various adjustments to centering the views, etc., but can't figure it out. What's going wrong?
I've add trouble with activity indicators in the past as well. Make sure you are not calling the startAnimating or stopAnimating while any animations are taking place. I recommend calling the startAnimating selector in viewDidLayoutSubviews.
The line where you set the center of the indicator looks like the source of the problem. self.view.frame.size is probably equal to screen size at this point and so when you show that view controller over the whole screen it's ok, but inside a split view controller it's not because indicator is off-bounds. You can check that from Xcode's Debug -> View Debugging -> Capture View Hierarchy (while the app is running).
Try setting activity indicator's center using autolayout and it should work.
I am interested in creating a menu bar like this:
From what I have been reading, I should be doing a UIScrollView. Are there any examples online that do this with infinite scrolling (wrapping around the menu bar) and the ability to have touch control? (i.e. swipe and it changes the menu bar).
Thanks!
Your request is very similar to mine. But I cannot find a good enough solution too. So I decide to create something to do that by myself.
The ACTabScrollView supports some different gestures. And the tabs and pages will always scroll synchronously.
Swipe pages normally
Drag tabs can quickly move pages
Click a tab to change to that page
The Infinite Scrolling feature is not ready yet, but I will keep to implement.
I use the similar UI as examples in this project. I don't know what the app is, but if using it as an example bother anyone. Contact me and I will immediately remove that.
Feel free to give me any suggestion to improve it :)
I hope this code will work for you.
- (void)viewDidLoad
{
[super viewDidLoad];
[self createHorizontalScroll];
}
- (void)createHorizontalScroll
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, 80)];
int buttonX = 0;
for (int i = 0; i < 5; i++)
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(buttonX, 0, 100, 60)];
[button setTitle:[NSString stringWithFormat:#"Button %d", i] forState:UIControlStateNormal];
[scrollView addSubview:button];
buttonX = buttonX+button.frame.size.width;
[button addTarget:self
action:#selector(changeView:)
forControlEvents:UIControlEventTouchUpInside];
}
scrollView.contentSize = CGSizeMake(buttonX, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor greenColor];
[self.view addSubview:scrollView];
}
-(void)changeView:(UIButton*)sender
{
NSLog(#"I Clicked a button %ld",(long)sender.tag);
}
You can set scroll content as much you want and menu buttons.Change the view according to the button click. Here the screen shot of the above code
I hope this might help you. This doesn't have more animation but works as you expected.
You can make use of CollectionView in this to scroll Horizontally with menu options as CollectionView Cell. For achieving the left and right swiping we can use UISwapeGuestureRecognizers and can scroll between pages.
Here is how i have implemented it using CollectionView for ScrollableMenu as tabs in the top of the screen and the Pages in the bottom of the menu using Container View.
You can download this project and check for getting idea about the implementation form the below link.
Horizontal Scrollable Menu
Hope this might help you. This is implemented with lates Swift 4.0 and X-Code 9.2
Happy Coding..
I edited example of #Shrikant K so it fits swift 5:
func createVerticalScroll() {
menuScrollView.frame = CGRect(x: 0, y: 0, width: 80, height: self.view.frame.size.height)
var buttonY: CGFloat = 0
for i in 0..<menuButtons.count {
menuButtons[i].frame = CGRect(x: 0, y: buttonY, width: 100, height: 60)
menuButtons[i].setTitle("Button "+String(i), for: .normal)
menuButtons[i].tag = i
menuScrollView.addSubview(menuButtons[i])
buttonY += menuButtons[i].frame.size.width
menuButtons[i].addTarget(self, action: #selector(menuButtonClicked), for: .touchUpInside)
}
menuScrollView.contentSize = CGSize(width: menuScrollView.frame.size.width, height: buttonY)
menuScrollView.backgroundColor = .green
menuScrollView.alpha = 0.5
view.addSubview(menuScrollView)
}
#IBAction func menuButtonClicked(_ sender: UIButton) {
print("I Clicked a button \(sender.tag)")
}
I have a static tableview with three rows I am creating programatically. I was creating it (incorrectly) in ViewDidAppear
CGRect fr = CGRectMake(10, 100, 280, 150);
SetUpTableView = [[UITableView alloc] initWithFrame:fr style:UITableViewStylePlain];
SetUpTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
SetUpTableView.delegate = self;
SetUpTableView.dataSource = self;
[self.view addSubview:SetUpTableView];
it was working fine.
I realized it was in the wrong location so i moved it to viewDidLoad
The table would NOT appear.
I commented out the auto resizing
CGRect fr = CGRectMake(10, 100, 280, 150);
SetUpTableView = [[UITableView alloc] initWithFrame:fr style:UITableViewStylePlain];
// SetUpTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
SetUpTableView.delegate = self;
SetUpTableView.dataSource = self;
[self.view addSubview:SetUpTableView];
and it now works fine in viewDidLoad.
I understand that I was in the wrong spot AND that this is a static table that doesn't need autoresizing but why would it work in viewDidAppear but not work in viewDidLoad?
It's all about timing.
viewWillAppear is called after the view hierarchy has finished being laid out. This means that whatever voodoo the auto resizing caused will be overridden by layout-related operations performed here.
viewDidLoad is called before the view has finished being laid out - this means that auto-resizing will occur after the code in viewDidLoad is executed.
Hope this clarifies things
In your viewDidLoad, how big is your view?
You have got magic numbers in your code - CGRectMake(10, 100, 280, 150);. If you want the tableview to be a fixed size from the left top right and bottom of your view, work it out, don't assume that you know the size of the view already!
Something like :
CGSize container = self.view.frame.size;
CGRect fr = CGRectMake(10, 100, container.width-60, container.height-100);
Otherwise, you might place your tableview outside the view and the autoresizing mask is just confused!
I have set up a UIScrollView with which I want to display 12 images (only 8 fit on screen) laid out horizontally. In the following image you can see the problem I'm having (which makes my scroll view not scroll), my constraints and the UIScrollView which I have added on storyboard:
I have called the following method on -(void)viewDidLoad, where I "set up"my scrollview (itemList is my scroll view property and itemNames a array with the images'names):
- (void)setupHorizontalScrollView
{
self.itemList.delegate = self;
[self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.itemList setBackgroundColor:[UIColor blackColor]];
[self.itemList setCanCancelContentTouches:NO];
self.itemList.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.itemList.clipsToBounds = NO;
self.itemList.scrollEnabled = YES;
self.itemList.pagingEnabled = NO;
NSInteger tot=0;
CGFloat cx = 0;
for (; ; tot++) {
if (tot==12) {
break;
}
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.itemNames objectAtIndex:tot]]];
CGRect rect = imageView.frame;
rect.size.height = 40;
rect.size.width = 40;
rect.origin.x = cx;
rect.origin.y = 0;
imageView.frame = rect;
[self.itemList addSubview:imageView];
cx += imageView.frame.size.width;
}
[self.itemList setContentSize:CGSizeMake(cx, [self.itemList bounds].size.height)];
}
I have added the [self.itemList setTranslatesAutoresizingMaskIntoConstraints:NO]; because I saw this suggestion on other posts, but it doesn't work with or without it. The only way it works is if I uncheck use AutoLayout on the storyboard, but that moves the UIImageViewI use to look as a navigation bar to the bottom of the screen.
I don't know what to do anymore, any help is appreciated :)
Try to set your scrollView's Content size int "viewDidLayoutSubviews" method with keeping the autolayouts set.
-(void)viewDidLayoutSubviews
{
[self.itemList setContentSize:CGSizeMake(required_width, required_height)];
}
Two Solutions:
Create different constraints that can be satisfied simultaneously (you will have to edit). I think the problem is your bottom space and top space constraints are mutually exclusive. please remove one and try again. IF this is difficult for you, try adding another UIView to contain the UIScrollView to help manage your constraints, it might seem odd at first, but sometimes adding another view to contain your view actually makes it simpler at each level.
Turn off Autolayout, and change the autoresize masks of your UIImageView to be what you wish.
Insert: [scrollView setContentSize:CGSizeMake(x,y)]; in the following method:
-(void)viewWillAppear:(BOOL)animated