Adding UIButton to subview not working - ios

I'm trying to add 16 UIButton in code to a subview of my main view:
internView = [[UIView alloc] initWithFrame:CGRectMake(16, 16, (self.view.frame.size.width - 16), (self.view.frame.size.height - 16) - 60)];
internView.backgroundColor = [UIColor whiteColor];
for (int rij = 0; rij < 4; rij++) //4 rows
{
for (int kolom = 0; kolom < 4; kolom++) //4 colomns
{
CGFloat x = (((width + spacing) * kolom) + spacing);
CGFloat y = (((height + spacing) * rij) + spacing);
int tag = ((4 * rij) + kolom);
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor blackColor];
[button setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[projects objectAtIndex:tag]thumbnailImage]]]] forState:UIControlStateNormal];
[button addTarget:self action:#selector(getDetail:) forControlEvents:UIControlStateNormal];
button.tag = tag;
NSLog(#"%i",button.tag);
[internView addSubview:button];
}
}
[self.view addSubview:internView];
The subview 'internview' is visible (as i made it white background) on the main view but the buttons aren't visible.
Also my log shows the tag of the button which goes from 0..15, so I know they are being made but simple aren't added to the subview 'internview'
Can't really see what I'm missing or doing wrong here..

Change your code to :
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(x, y, width, height);
In your version you have:
Memory leak because you have created buttons two times
[UIButton buttonWithType:UIButtonTypeCustom]; returns new button
with CGRectZero frame

Related

UIView coordinate conversion has an exception. convertPoint: toView: not work well

I try to make a navigation bar indicator view animation. The layout has some problem.
Expects this:
My solution is to convert the position in the navigation bar's bottom position to self.navigationItem.titleView.
The navigation bar indicator view should belong to the view controller. Not the navigation controller. So indicator view is on self.navigationItem.titleView.
While its position is the navigation bar's bottom. Y position is Point (0, 64) exactly.
So a method convertPoint: toView: is what I need .
Here is code:
- (void)setupTitlesView
{
UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
self.navigationItem.titleView = titlesView;
CGPoint new_point = [self.view convertPoint: CGPointMake(0, 64) toView: self.navigationItem.titleView];
NSLog(#"\n\n%#\n\n", NSStringFromCGPoint(new_point));
UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, new_point.y - 2, 0, 2)];
indicatorView.tag = -1;
self.indicatorView = indicatorView;
NSArray *titles = #[#"商品", #"详情"];
CGFloat width = titlesView.frame.size.width / titles.count;
CGFloat height = titlesView.frame.size.height;
for (NSInteger i = 0; i<titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
button.tag = i;
[button setTitle:titles[i] forState:UIControlStateNormal];
// color , font ...
[titlesView addSubview:button];
// default click
if (i == 0) {
button.enabled = NO;
self.selectedButton = button;
[button.titleLabel sizeToFit];
CGRect tempFrame = self.indicatorView.frame;
tempFrame.size.width = button.titleLabel.frame.size.width;
self.indicatorView.frame = tempFrame;
CGPoint tempCenter = self.indicatorView.center;
tempCenter.x = button.center.x;
self.indicatorView.center = tempCenter;
}
}
[titlesView addSubview: indicatorView];
}
The result prints :
{0, 64}
Apple says:
convertPoint:toView: Converts a point from the receiver’s coordinate
system to that of the specified view.
How to fix it?
If I were you,I would try:
- (void)setupTitlesView
{
UIView *titlesView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 100, 35)];
self.navigationItem.titleView = titlesView;
UIView *indicatorView = [[UIView alloc] initWithFrame: CGRectMake(0, titlesView.frame.size.height - 2, 50, 2)];
indicatorView.tag = -1;
self.indicatorView = indicatorView;
self.indicatorView.backgroundColor = [UIColor blueColor];
NSArray *titles = #[#"商品", #"详情"];
CGFloat width = titlesView.frame.size.width / titles.count;
CGFloat height = titlesView.frame.size.height;
for (NSInteger i = 0; i<titles.count; i++) {
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(i*width, 0, width, height)];
button.tag = i+1000;
[button setTitle:titles[i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
[titlesView addSubview:button];
// default click
if (i == 0) {
button.selected = YES;
}
}
[titlesView addSubview: indicatorView];
}
- (void)buttonClicked:(UIButton *)sender
{
for (int i = 0; i < 2; i++) {
UIButton *button = [self.navigationItem.titleView viewWithTag:(1000+i)];
button.selected = (button.tag == sender.tag);
}
[UIView animateWithDuration:0.3 animations:^{
self.indicatorView.center = CGPointMake(sender.center.x, self.indicatorView.center.y);
}];
}

Bringing UiScrollView element to center when clicked

What I have is UIScrollView and inside it I have buttons of different different sizes.
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView.frame = CGRectMake(0, 100, 375, 50.0);
int totalButtonsToAdd = 20;
startX = 0;
int buttonWidth = 60;
for (int i=0;i<totalButtonsToAdd;i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"Btn-%d", i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
button.accessibilityValue = [NSString stringWithFormat:#"%d", i+1];
button.frame = CGRectMake(startX, 0, buttonWidth, 50.0);
[[button layer] setBorderWidth:2.0f];
[[button layer] setBorderColor:[UIColor greenColor].CGColor];
button.layer.cornerRadius = 5;
startX = startX + buttonWidth + 5;
buttonWidth = buttonWidth + 5;
[myScrollView addSubview:button];
}
NSLog(#"startX===%d", startX);
myScrollView.contentSize = CGSizeMake(startX, 50.0);
}
-(IBAction)aMethod:(id)sender {
UIButton *mButton = (UIButton *)sender;
NSLog(#"clicked button index is %#", mButton.accessibilityValue);
}
What I want to do is when I click any button, I want to bring that button in center. For this I believe I will need to scroll the scrollview to specific position.
For this what I did is as below.
- (void)viewDidLoad {
[super viewDidLoad];
myScrollView.frame = CGRectMake(0, 100, 375, 50.0);
int totalButtonsToAdd = 20;
startX = 0;
int buttonWidth = 60;
for (int i=0;i<totalButtonsToAdd;i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[NSString stringWithFormat:#"Btn-%d", i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
button.accessibilityValue = [NSString stringWithFormat:#"%d", i+1];
button.frame = CGRectMake(startX, 0, buttonWidth, 50.0);
[[button layer] setBorderWidth:2.0f];
[[button layer] setBorderColor:[UIColor greenColor].CGColor];
button.layer.cornerRadius = 5;
startX = startX + buttonWidth + 5;
NSLog(#"startX===%d==%d", i, startX);
buttonWidth = buttonWidth + 5;
[myScrollView addSubview:button];
}
NSLog(#"startX final===%d", startX);
myScrollView.contentSize = CGSizeMake(startX, 50.0);
}
-(IBAction)aMethod:(id)sender {
UIButton *mButton = (UIButton *)sender;
NSLog(#"clicked button index is %#", mButton.accessibilityValue);
focusPosition = 0;
int totalButtonsToAdd = 20;
int buttonWidth = 60;
startX = 0;
for (int i=0;i<totalButtonsToAdd;i++) {
startX = startX + buttonWidth + 5;
buttonWidth = buttonWidth + 5;
if (i==[mButton.accessibilityValue intValue]) {
focusPosition = startX;
}
}
NSLog(#"focusPosition===%d", focusPosition);
CGRect frame = CGRectMake(focusPosition, 0, buttonWidth, 50); //wherever you want to scroll
[myScrollView scrollRectToVisible:frame animated:NO];
}
However this don't give me exact what I want. Is there any property for scrollview that will bring that button in center?
Project with above code
Note: This is sample for iPhone 6 only. So please run project on iPhone 6 simulator only
UICollectionView would probably have been a better choice than a UIScrollView, since it adds a method to achieve just what you want (scrollToItemAtIndexPath:atScrollPosition:animated:).
If you're sticking with UIScrollView, you'll need to use setContentOffset:animated: instead, and compute the desired offset, e.g.:
-(IBAction)aMethod:(UIButton *)button {
CGPoint contentOffset = CGPointMake(CGRectGetMidX(button.frame) - (CGRectGetWidth(self.scrollView.frame) / 2.0),0.0);
[myScrollView setContentOffset:contentOffset animated:YES];
}
The UIScrollView function
- (void)scrollRectToVisible:(CGRect)rect
animated:(BOOL)animated
does almost that.
Reference says: "This method scrolls the content view so that the area defined by rect is just visible inside the scroll view. If the area is already visible, the method does nothing."
Sorry, just saw you've already found that function.
Here is the link of a sample project which is doing the same as you want.
This include one class named PagedFlowView.
sample project to center a view in scrollview

UIScrollView needs to get bigger every time I add a button

The scrollView is created in my storyboard and is horizontal. Here is the method where I add a button for every loop and need to increase the size of the scrollView every time a button is added.
- (void) populateGroups {
NSInteger x = [self.groupNameArray count];
for (int i = 0; i < x; i++) {
NSString *name = [self.groupNameArray objectAtIndex:i];
// Create Button
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(count, 0, 100, 54)];
[button setTitle:name forState:UIControlStateNormal];
button.titleLabel.font = [UIFont systemFontOfSize:10];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor:[UIColor whiteColor].CGColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(groupPage:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.groupScrollView addSubview:button];
count = count + 100;
// Increase ScrollViewSize
self.groupScrollView.frame = CGRectMake(0, 0, frameWidth, 80);
frameWidth = frameWidth + 100;
}
}
The scrollView stays the same when I try to add two buttons to the view.
you have to increase the content size of the scroll view like:
self.groupScrollView.contentSize = CGSizeMake(frameWidth, 80);
and better to do it after the loop.

create horizontal scrolling UIButton in iOS using UIScrollView in ios 7

I'm new to ios development now want give a horizontal scrolling button in my app i dont no wt is the correct method to do it can any pls tell how to make it
this is the code i have tried to create a uibutton horizontal pro grammatically but its now working its throughing a warning
undeclared selector newView
i dont know wt i have to code in newview to make the button scroll im using more than four button i want all the buttons to be scroll in horizontal but i not able to make i have tried to create using one button
this is the code i have used in the view didload
UIScrollView *scrollview =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollview.showsHorizontalScrollIndicator=YES;
scrollview.userInteractionEnabled=YES;
scrollview.scrollEnabled=YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Excavations" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);
can any one pls tell me wt is the right way to make the horizontal scroll wheather i have use the uiscorllview and buttons in my storyboard and how to make into scroll the buttons
i have no idea to make im very confused by seening many tutorial sampel like thisone link
Follow my code:
CGFloat btnX = 80.0;
int numberOfButton = 10;
for (int i = 1 ; i <= numberOfButton; i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(newView:) forControlEvents:UIControlEventTouchDown];
button.tag = i;
[button setTitle:#"Excavations" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[scrollview addSubView:button];
btnX = btnX + 165.0;
}
scrollview.contentSize = CGSizeMake(btnX + 50, yourHeight);
Your warning says that you forgot to add method of your UIButton, so you need to declare method of button, such like..
-(void) newView:(UIButton *) sender
{
//you can access your button by it's tag.
}
- (void)createScrollViewMenu
{
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
int x = 0;
for (int i = 0; i < 10; i++) {
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, 100, 100)];
[button setTitle:[NSString stringWithFormat:#"NameOfBtn %d", i] forState:UIControlStateNormal];
[button addTarget:self action:#selector(BtnClicked:) forControlEvents:UIControlEventTouchDown];
button.tag = i;
[scrollView addSubview:button];
x += button.frame.size.width;
}
scrollView.contentSize = CGSizeMake(x, scrollView.frame.size.height);
scrollView.backgroundColor = [UIColor redColor];
[self.view addSubview:scrollView];
}
This will create a scrollview with a height of 100, width as big as its parent, and add 10 buttons to it.
so you need to declare method of button, such like..
-(void)BtnClicked:(UIButton *) sender
{
//you can access your button by it's tag.
}

Infinite horizontal UIScrollView with UIButtons

here is what I do at the moment.
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(56, 205, 400, 3000)];
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f,scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
The problem is. It's not inifinite. I would like the number 101 to be the number 1, hope you understand. And how could i create them dynamically, with a webservice telling me how much button i will have, what background i should apply to each button, and what text to put under the button. Also the code above is scrolling vertically, but at the end it would be a non zooming horizontal scrolllview. Thanks in advance.
Just set content size of your scrollview
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(56, 205, 400, 320)]; // it is for iphone. set it's resolution if you want to set it for ipad
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * 3.0f,scrollView.frame.size.height * 3.0f);
scrollView.maximumZoomScale = 3.0f;
UIView *zoomView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, scrollView.contentSize.width, scrollView.contentSize.height)];
zoomView.backgroundColor = [UIColor whiteColor];
for (NSInteger index = 0; index < 100; index++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake((scrollView.frame.size.width / 2.0f) - 50.0f, 10.0f + (50.0f * (CGFloat)index), 100.0f, 30.0f);
button.tag = index;
[button setTitle:[NSString stringWithFormat:#"Button %ld", ((long)index + 1)] forState:UIControlStateNormal];
[button addTarget:self action:#selector(didTapButton:) forControlEvents:UIControlEventTouchUpInside];
[zoomView addSubview:button];
}
[scrollView addSubview:zoomView];
[self.view addSubview:scrollView];
scrollView.contentSize= CGSizeMake((zoomView.frame.size.width) , zoomView.frame.size.height);
set it after your zoom view has been added into scrollView.
I hope it helps you to understand.

Resources