I am adding a series of buttons to my scrollview,So on clicking button I want to highlight that one and deselect remaining all. So every thing Works fine. But i need one more behaviour like if Second button is highlighted I want to show some part of third button so that user knows there is one more category.
float offset=0;
for(int i=0;i<[arrMaincat count];i++)
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.tag=i;
btn.titleLabel.font=[UIFont systemFontOfSize:12];
btn.titleLabel.lineBreakMode=UILineBreakModeTailTruncation;
NSString *Categoryname=[[arrMaincat objectAtIndex:i] objectForKey:#"Name"];
CGSize constraint=CGSizeMake(1000, 30);
CGSize size1 = [Categoryname sizeWithFont:[UIFont systemFontOfSize:12] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
btn.frame=CGRectMake(offset, 0, size1.width, 30);
[btn setTitle:Categoryname forState:UIControlStateNormal];
[btn setTitle:Categoryname forState:UIControlStateSelected];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn setTitleColor:UIColorFromRGB(0Xe08043) forState:UIControlStateSelected];
[btn addTarget:self action:#selector(CategoryChanged:) forControlEvents:UIControlEventTouchUpInside];
offset=btn.frame.origin.x+btn.frame.size.width+10;
[self.SliderScroll addSubview:btn];
}
self.SliderScroll.contentSize=CGSizeMake(offset, SliderScroll.frame.size.height);
Remaning Code http://pastie.org/10277453
Here is my screenshot https://www.dropbox.com/s/xpq9vy39eecab1g/Screen%20Shot%202015-07-07%20at%205.34.15%20pm.png?dl=0
Thank you
So whats the problem, its a simple calculation of content offset. If the button being highlighted is the last button in the scroll view's current visible frame you can add some hardcoded value (lets say 50) to the content offset.x
Related
I have created a UIScrollview programmatically. And I have added some programmatically created buttons onto this scroll view. Button height is 30. But the button width changing according to the screen size.
float scrollYearX=btnLeftArrow.frame.origin.x+btnLeftArrow.frame.size.width+15;
float scrollYearY=40.0;
float scrollYearWidth=btnRightArrow.frame.origin.x-15-scrollYearX;
float scrollYearHeight=50.0;
scrollYear=[[UIScrollView alloc] initWithFrame:CGRectMake(scrollYearX, scrollYearY, scrollYearWidth, scrollYearHeight)];
[scrollYear setShowsHorizontalScrollIndicator:NO];
[scrollYear setShowsVerticalScrollIndicator:NO];
[vwParent addSubview:scrollYear];
int buttonX=0;
for (int i=0; i<100; i++) {
UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(buttonX, 0, scrollYearWidth/5, 30)];
[btn.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue" size:14.0]];
int mod=i%2;
if (mod==1) {
[btn setTitle:#"-" forState:UIControlStateNormal];
[btn.titleLabel setTextColor:[UIColor whiteColor]];
}
else
{
[btn addTarget:self action:#selector(yearClick :) forControlEvents:UIControlEventTouchUpInside];
[btn setTitle:[NSString stringWithFormat:#"%li",(long)backYear] forState:UIControlStateNormal];
if (backYear==year) {
btnCurrentYear=btn;
[btn setBackgroundImage:[UIImage imageNamed:#"yearSelect"] forState:UIControlStateNormal];
}
backYear++;
}
[scrollYear addSubview:btn];
btnLast=btn;
buttonX=buttonX+btn.frame.size.width;
}
[scrollYear setContentSize:CGSizeMake(btnLast.frame.origin.x+btnLast.frame.size.width, scrollYearHeight)];
[scrollYear setContentOffset:CGPointMake(btnCurrentYear.frame.origin.x-(btnLast.frame.size.width*2),0)];
My problem is which constraint I should use inorder to display this correctly in every device. Here I have attached how it shows on 6s and ipad pro. How can I make the ipad pro visual same as the 6s.
Button loks in 6s
Button looks in ipad pro
Please help me.
Thanks
This is because your button background width got scaled. you need to use resizable image to keep the round edge. Change your background image with follow:
UIImage* backgroundImage = [UIImage imageNamed:#"yearSelect"];
backgroundImage = [backgroundImage resizableImageWithCapInsets:UIEdgeInsetsMake(0, backgroundImage.size.width/2-1, 0, backgroundImage.size.width/2)];
[btn setBackgroundImage:backgroundImage forState:UIControlStateNormal];
I have a UITableView and 10 sections in it.
For each section I have a custom header.
In custom header I have a label as heading of section with text value date.
Table content size is (450,1000).
Here my label with text is also scrolling, but I want my label text should aligned center to screen and it should not scroll horizontally with table.
Thanks in advance.
After knocking my head for hours finally i got this solution.
Simply i used
(void)scrollViewDidScroll:(UIScrollView *)scrollView*
method and changed my label frame with table view content offset.
myLabel.frame = CGRectMake(tableview.contentoffset.x, 0, deviceHeight, 27);
It will keep my label at same position to screen, logically it is changing but will look like its stable at same position.
That behavior is only common when the UITableViewStyle property of the table is set to UITableViewStylePlain. If you have it set to UITableViewStyleGrouped, the headers will scroll up with the cells.
May be the following links will help you to solve your problem:
1. UITableView with fixed section headers
2. https://corecocoa.wordpress.com/2011/09/17/how-to-disable-floating-header-in-uitableview/
- (void)addButtonsOnScrollViewHorizontal:(int)postion :(UIScrollView*)scrollView
{
[scrollView setShowsVerticalScrollIndicator:NO];
float x = 30;
for (int i = 1; i <=postion; i++)
{
UIButton *button= [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:i];
[button setTitle:[NSString stringWithFormat:#"%i",i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithRed:26.0f/255.0f green:188.0f/255.0f blue:156.0f/255.0f alpha:100.0f] forState:UIControlStateSelected];
button.titleLabel.font =[UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0f];
CGSize maxSize = CGSizeMake(button.frame.size.width, FLT_MAX);
CGRect labRect = [button.titleLabel.text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName:button.titleLabel.font} context:nil];
[button setFrame:CGRectMake(x, 0, labRect.size.width+10, 30)];
[button addTarget:self action:#selector(scrollViewDaysbtnClicked:) forControlEvents:UIControlEventTouchUpInside];
if (i==postion)
{
[_scrollViewDays setContentOffset:CGPointMake (button.frame.origin.x+button.frame.size.width/2-70, 0) animated:NO];
button.titleLabel.font =[UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
[button setSelected:YES];
currentSelectionDays =i;
}
[scrollView addSubview:button];
x +=labRect.size.width+20;
}
scrollView .contentSize = CGSizeMake(x, 0);
}
-(void)scrollViewDaysbtnClicked:(UIButton*)sender
{
for (UIButton *button in _scrollViewDays.subviews)
{
if ([button isKindOfClass:[UIButton class]])
{
if (button.tag == sender.tag)
{
[_scrollViewDays setContentOffset:CGPointMake (button.frame.origin.x+button.frame.size.width/2-70, 0) animated:NO];
button.titleLabel.font =[UIFont fontWithName:#"HelveticaNeue-Bold" size:24.0f];
[button setSelected:YES];
currentSelectionDays =(int)sender.tag;
}else
{
[button setSelected:NO];
button.titleLabel.font =[UIFont fontWithName:#"HelveticaNeue-Bold" size:18.0f];
}
}
}
}
use it....
I want to center (both x and y axis) this + sign within my UIButton CGRect but it keeps getting pushed down the y-axis.
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeSystem];
[aButton setTitle:#"+" forState:UIControlStateNormal];
[aButton.titleLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Bold" size:(40.0)]];
[aButton setBackgroundColor:[UIColor grayColor]];
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
aButton.layer.cornerRadius = 25.0;
aButton.frame = CGRectMake(75.0, 100.0, 50.0, 50.0);
This would look more correct to you if you had ascenders and descenders. There's a label inside the button and it is sized to accommodate the height of the full range of possible characters of this font. If you don't like the position of the label, you're free to move it. There are a lot of ways to do this; you might try playing with the button's titleEdgeInsets, for example.
Add this line after setting the title and font:
aButton.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 9, 0);
I have a view which has a label on extreme left, then a button with username and then his comment after the button on the right. What I want to do is to position Label where text of UIButton is ending. In this case, if username is long or short, comment will start without any space between the username button and comment label. I am currently doing hard coded like this, how can I achieve dynamic position of UILabel depending upon size of text of UIButtion?, PfButton is subclass of UIButton Thanks
PfButton *button = [PfButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:name forState:UIControlStateNormal];
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentEdgeInsets:UIEdgeInsetsMake(0, 13, 0, 0)];
[button setObjectId:objId];
[button addTarget:self action:#selector(profilePhotoButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(30, -7, 130, 20)];
[[button titleLabel] setTextAlignment:NSTextAlignmentLeft];
[view addSubview:button];
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, -7, 150, 20)];
[messageLabel setFont:[UIFont systemFontOfSize:15]];
[messageLabel setText:msg];
[messageLabel setTextAlignment:NSTextAlignmentLeft];
[view addSubview:messageLabel];
[messageLabel release];
sizeToFit doesn't work well if you want to have your button be wider than the label, or if you change one of the many sizing properties on the button. A better solution is to simply translate the titleLabel's coordinate system to the buttons superview.
CGRect buttonTitleFrame = button.titleLabel.frame;
CGRect convertedRect = [button convertRect:button.titleLabel.frame toView:button.superview];
CGFloat xForLabel = convertedRect.origin.x + buttonTitleFrame.size.width;
CGRect currentLabelFrame = label.frame;
CGRect labelFrame = CGRectMake(xForLabel, currentLabelFrame.origin.y, currentLabelFrame.size.width, currentLabelFrame.size.height);
label.frame = labelFrame;
The second line is the key one here. You ask the button to convert a rectangle that is within it (in this case the title labels) to what it would be in another view (in this case the buttons superview, which is probably your view controllers self.view).
Line 3 takes the translated origin and adds the labels exact width, and 5 uses the values from the labels frame except the x, which is our calculated value.
Got it to work by using
[button sizeToFit];
It gave me the exact frame, then calculated the position of label by taking into account button's x position and width
In my cellForRowAtIndex I initialize a button (it gets shown in every cell in the tableview):
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0f,cell.frame.size.height -15, 160.0f, 15.0f);
UIImage *buttonImage = [UIImage imageNamed:#"Image1.png"];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button.titleLabel setFont:[UIFont fontWithName:#"Arial" size:13.0]];
button.contentEdgeInsets = UIEdgeInsetsMake(1, 60, 0, 0);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[cell addSubview:button];
button.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
It has a normal image and if the user clicks on the button, the image changes. Also I get the contents of the button.title from a XML. Therefore I tell it to get the right title from the web. This works fine, but sometimes (not all the time) and also randomly it changes the button from the next cell, or the cell three rows down. I don't know why and couldn't find anything while running with breakpoints/debugger.
-(void)customActionPressed :(id)sender
{
UITableViewCell *owningCell = (UITableViewCell*)[sender superview];
NSIndexPath *pathToCell = [self.tableView indexPathForCell:owningCell];
UIImage *buttonImage = [UIImage imageNamed:#"image2.png"];
[self doXMLParsing];
[sender setBackgroundImage:buttonImage forState:UIControlStateNormal];
NSString *TableText = [[NSString alloc] initWithFormat:#"%#", [[tabelle objectAtIndex:pathToCell.row] pressed1]];
[sender setTitle:TableText forState:UIControlStateNormal];
((UIButton *)sender).enabled = NO;
}
So maybe someone can see where I went wrong. If you need more code, just say so.
[button addTarget:self action:#selector(customActionPressed:)
forControlEvents:UIControlEventTouchDown];
Try having UIControlEventTouchUpInside instead of UIControlEventTouchDown.
also you create a custom cell and then you can create a property in the class subclassing UITableViewCell for your button and change the image of button through that property.
Also Don't change the image from action of button .You can access the button through cell.yourbuttonName in didSelectRowAtIndexPath delegate of tableview and then change the image.