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)")
}
Related
In our project we wanted to change the size of navigationbar. Have achieved it with
#implementation UINavigationBar (customNav)
- (CGSize)sizeThatFits:(CGSize)size {
return CGSizeMake(self.superview.bounds.size.width, 55.0f);
}
#end
But the problem here is.. as you would see go button is little down from screen edge. But I wanted it to be in ultimate corner of screen. I use backgroundColor:, setTitle:forState:methods for constructing look of go button. also back button to be vertically middle. How can I achieve that. Tried Using edgeInsets: but no hope.
Edit: Go button height is 10px less than barheight.
if I give equal height. Below is the result
in somewhat I have tried your coding.Check that
UIButton *goBtn = [UIButton buttonWithType:UIButtonTypeCustom];
goBtn.frame = CGRectMake(0, 0, 63, 80);
goBtn.backgroundColor = [UIColor redColor];
UIView *goButtonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 63, 50)];
NSLog(#"goButtonView bounds are - %#", NSStringFromCGRect([goButtonView bounds]));
goButtonView.bounds = CGRectOffset(goButtonView.bounds, -17, 0);
NSLog(#"goButtonView bounds are - %#", NSStringFromCGRect([goButtonView bounds]));
[goButtonView addSubview:goBtn];
UIBarButtonItem *goButton = [[UIBarButtonItem alloc] initWithCustomView:goButtonView];
self.navigationItem.rightBarButtonItem = goButton;
Below the references
Issue with button on the left side of the uinavigation bar
UINavigationBar UIBarButtonItems much larger click area than required
I am under one project.In that I created one uibutton at bottom of my viewcontroller using code (Not via storyboard).But when i run my app,my uibutton are placing in differents positions.for example in iphone 5,6 simulator are in some position.In 4s,ipad simulator my uibutton is unvisible.Also i check with my own device(iphone 6) ,in that also my uibutton is not able to see.
Needed
I used below code for creating & placing my button position.but its not fit in one position.
CGFloat buttonSize = 40;
UIButton * disButton = [[UIButton alloc] init];
disButton.backgroundColor = [UIColor redColor];
UIImage *btnImage = [UIImage imageNamed:#"23"];
[disButton setImage:btnImage forState:UIControlStateNormal];
disButton.frame = CGRectMake(150, 523, 40, 40);
[overlayView addSubview:disButton];
[disButton addTarget:self action:#selector(dismissPopUpViewController)
forControlEvents:UIControlEventTouchUpInside];
In my code i use to place position in this line:
disButton.frame = CGRectMake(150, 523, 40, 40);
=> x-position = 150
=> Y-position = 523
Kindly please give some code solution for this problem.
As per all comments about your question.You need to set constraint for your UIButon.Use only centre x,centre y because that will helpfull to see your layout in all devices. And there is a already solution for your problem.see these below links for setting auto layout for UIButton
Link 1
Link 2
Link 3
Sure this will help you !
I'm creating a simple button and adding it as a subview to main view. It doesn't show up though.
This is the code for setting it up:
UIButton* contactButton = [[UIButton alloc] init];
contactButton.titleLabel.text = #"Contact Developer";
[contactButton sizeToFit];
[self.view addSubview:contactButton];
NSLog(#"X: %f || Y: %f || Width: %f || Height: %f", contactButton.frame.origin.x, contactButton.frame.origin.y, contactButton.frame.size.width, contactButton.frame.size.height);
As you may have noticed, I placed a bit of debug code at the end to see the position and dimensions of the button. They are: X: 0, Y: 0, Width: 30, Height: 34
This means it should be showing up in the upper left corner but it doesn't. Whats wrong?
One possible reason for this is that you used titleLabeldirectly. Consider using setTitle:forState: method instead.
To be sure, consider setting backgroundColor, as a debug step, to make sure it's appearing.
Edit As others suggested, try using buttonWithType: instead of [[UIButton alloc] init]. I suggest using UIButtonTypeSystem instead of UIButtonTypeCustom.
You should initialise the button using the following:
UIButton* contactButton = [UIButton buttonWithType:UIButtonTypeCustom];
Try constructing your button a different way:
UIButton* contactButton = [UIButton buttonWithType:UIButtonTypeCustom];
contactButton.backgroundColor = [UIColor redColor];
contactButton.titleLabel.text = #"Contact Developer";
[contactButton sizeToFit];
[self.view addSubview:contactButton];
Are you sure that the button it's not being shown?
I think you are setting the title of the button incorrectly and because of the fact that a default UIButton has clear background and white title, it´s not visible if your superview is white too.
Instead of setting:
contactButton.titleLabel.text = #"Contact Developer"
use:
[contactButton setTitle:#"Contact Developer" forState:UIControlStateNormal]
But you can first try to set a backgroundColor to the button, different that the superview, to see if it's added.
Hope it helps!
(Sorry for my english if i have a mistake)
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];
I am creating a modal view in an App, which contains several subviews. Each one of those subviews has a button and an action related to that button. I use a loop to insert each subview in place. The problem is that the first 4 subviews are ok, from the 5th to the last there are no responses.
Here it is the simplified code related to the problem:
Simplified SubviewView.m
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// More info button
CGRect buttonFrame = CGRectMake(infoMarginLeft + infoWidthWithInfo, infoHeightControl, 25, 25);
UIButton * button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[button setFrame:buttonFrame];
[button addTarget:self action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
}
return self;
}
-(void)buttonTapped:(id)sender
{
NSLog(#"Button tapped!";
}
Simplified view.m
- (void)drawRect:(CGRect)rect
{
for (int i = 0; i < 12; i++) {
// Color info container
SubviewView * miniView = [[SubviewView alloc] initWithFrame:CGRectMake(0, 20 * i, 15, 15)];
miniColorView.backgroundColor = [UIColor clearColor];
// Adding subview through array to keep track of objects
[self addSubview:miniColorView];
}
}
Thanks in advance, I have absolutely no ideia of what's going on :p
--
EDIT
Just found out what it was!
The parent view had as height the screen height. The thing is, some of the subviews were beyond the screen height limit. All I had to do was increase the parent view height =)
I would check to make sure you are not adding your view miniView over top of your button.
Also this should be moved outside of drawRect, just put this in your init method.
Edit:
Set your miniView's background color to another besides clear, and see if you still see your button. If not, then you covered up your button, and it won't receive touch events.