how to make UIView work as an scrollview - ios

I need some serious help on this problem of mine where I need to work a UIView as an ScrollView. Below uploaded picture will clear all the doubts.
I have taken four buttons along with a UIView below them along with 4 container views which I have placed one above the other so as to work along with the four buttons.. What I want now is to work UIView as an ScrollView. Whenever I click any button, the View moves forward as well as the it changes the container view. And respective container view shows whenever I click a button.

Hi better you can use UIScrollView instead of UIView like:
//Declare it
NSMutableArray * buttonArray
int selectedIndex;
-(void)setUpSegmentBar {
NSArray * namesOfMenus =#[#"Button1",#"Button2",#"Button3",#"Button4",#"Button5"];
// Scrollview added on storyBoard
myScrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 2, self.view.frame.size.width, 40)];
CGFloat scrollWidth = 0.f;
buttonArray=[[NSMutableArray alloc]init];
for ( int j=0; j<namesOfMenus.count; j++)
{
NSString * name =[namesOfMenus objectAtIndex:j];
CGSize size = [name sizeWithAttributes:
#{NSFontAttributeName: [UIFont fontWithName:font_bold size:font_size_button]}];
CGSize textSize = CGSizeMake(ceilf(size.width), ceilf(size.height));
CGFloat strikeWidth;
if (iPAD) {
strikeWidth = self.view.frame.size.width/5.5;
}else{
strikeWidth = textSize.width;
}
CGRect frame = CGRectMake(scrollWidth, 0,strikeWidth+20, 40);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:j];
button.frame = frame;
[button setBackgroundColor:[UIColor whiteColor]];
button.titleLabel.textColor=[UIColor whiteColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
button.layer.borderColor=[UIColor whiteColor].CGColor;
button.titleLabel.textAlignment=NSTextAlignmentCenter;
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:name forState:UIControlStateNormal];
// ScrollWidth As per Button Width
scrollWidth= scrollWidth+strikeWidth+20;
// to Check Which Button Is Selected.
if (j==selectedIndex) {
// If Selected Do UI Changes
button.backgroundColor=segment_selected_Color ;
[button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
[button addShaddow];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}else {
// Other Buttons
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
button.backgroundColor=segment_disselected_Color;
[button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else{
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}
// Add Buttons in Array
[buttonArray addObject:button];
// Add button on Scroll View
[myScrollView addSubview:button];
}
myScrollView.contentSize = CGSizeMake(scrollWidth, 30.f);
myScrollView.pagingEnabled = NO;
myScrollView.backgroundColor=[UIColor whiteColor];
[myScrollView setShowsHorizontalScrollIndicator:NO];
[myScrollView setShowsVerticalScrollIndicator:NO];
return myScrollView;
}
And For Handeling button action.
-(void)buttonEvent:(UIButton*)sender
{
NSInteger index= sender.tag;
selectedIndex= (int) index;
for(int i=0;i<buttonArray.count;i++)
{
UIButton * button =(UIButton*)[buttonArray objectAtIndex:i];
if (i==selectedIndex) {
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
button.backgroundColor=segment_selected_Color ;
[button setTitleColor:segment_disselected_Color forState:UIControlStateNormal];
[button addShaddow];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}else {
button.backgroundColor=segment_disselected_Color;
[button addLayerAndCornerRadius:0 AndWidth:1 AndColor:segment_disselected_Color];
[button setTitleColor:segment_selected_Color forState:UIControlStateNormal];
if (iPhone6||iPhone6plus) {
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}else{
button.titleLabel.font=[UIFont fontWithName:font_bold size:font_size_normal_regular];
}
}
}
CGRect frame1 = myScrollView.frame;
UIButton * bt=(UIButton*)[buttonArray objectAtIndex:index];
frame1 =bt.frame ;
// By using it button will scroll and show properly
[myScrollView scrollRectToVisible:frame1 animated:YES];
[self setupTableAfterClick];
}

Related

getting element from array one by one and createbutton

I have a UItextFeild as search bar on clicking a tableview with suggestion open.now i want to click on that row and make a button with its label in below view as described in image .how can i acheive this
I want it one by one means when i click on row it create a button on view and UITableView hide then again i start searching and on again clicking on some other row make a button with different value of X.
_selectednames = [NSMutableArray arrayWithCapacity:self.sortedArray.count];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// MGContactItemsModel *model = _arrayForContacts[indexPath.row];
self.searchText.text = [NSString stringWithFormat:#"%#",[_sortedArray objectAtIndex:indexPath.row]];
self.tableView.hidden = YES;
[_selectednames addObject:_searchText.text];
for (int i; i < _selectednames.count ; i++) {
[self makeLabelsAndButtons:i];
}
now in my makeLabelsAndButtons funtion i done something like this
CGFloat xOffset = 10.0f;
int buttonCount = 0;
CGRect buttonFrame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
NSString *nameString = [self.selectednames objectAtIndex:indexPath];
UIButton *_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
//_button.frame = CGRectMake(xOffset, 10.0, 50.0, 15.0);
for(int i=buttonCount; i >0;i++) {
xOffset = xOffset+10;
buttonFrame.origin.x += buttonFrame.size.width+xOffset;
}
_button.frame = buttonFrame;
buttonCount++;
_button.tag = indexPath;
//button.center = CGPointMake(xOffset, 10.0f);
//[button setBackgroundImage:[UIImage imageNamed:picString] forState: UIControlStateNormal];
[_button setTitle:self.selectednames[indexPath] forState:UIControlStateNormal];
_button.backgroundColor = [UIColor lightGrayColor];
_button.titleLabel.font = [UIFont fontWithName:#"Arial" size:8.0f];
_button.layer.cornerRadius = 10.0f;
_button.layer.masksToBounds = YES;
[self.buttonView addSubview:_button];
NSLog(#"name: %#", nameString);
also on clicking button it will removed.any help would be thankful.
It works for me.Please try it.
y=20;
x=20;
for (int i=0; i<[arrAssignUsers count]; i++) {
CGRect screenRect=[[UIScreen mainScreen]bounds];
CGFloat screenWidth=screenRect.size.width;
[arrBtnStatus addObject:[NSNumber numberWithBool:NO]];
NSString *strNames=[arrAssignUsers objectAtIndex:i];
stringsize=[strNames sizeWithAttributes:
#{NSFontAttributeName: [UIFont systemFontOfSize:20.0f]}];
btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
CGFloat m=x+stringsize.width+10;
CGFloat n=screenWidth-20;
if (m<=n) {
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width +10;
}
else
{
y=y+stringsize.height+10;
x=20;
btn.frame=CGRectMake(x, y,stringsize.width,stringsize.height);
x=x+stringsize.width+10;
}
[btn setTitle:self.arrAssignUsers[i] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
btn.tag=i;
[btn addTarget:self
action:#selector(notifyButtonPressedInEditTask:) forControlEvents:UIControlEventTouchUpInside];
btn.backgroundColor=WHITE_COLOR;
btn.layer.cornerRadius=10;
[btn.layer setMasksToBounds:YES];
btn.layer.borderWidth=2.0f;
btn.layer.borderColor=[UIColor orangeColor].CGColor;
[self checkNotifybtnStatus:btn];
[cell addSubview:btn];
}
}
It may helps to you.Thank you
You can use UICollectionView to create buttons. All you need to do is
1. Create custom cell with button as you want
2. When you tap on table view cell, add item to array and reload UICollectionView
3. Show buttons and it will automatically add scrollview for you.
When you tap on UICollectionView cell, remove object from array and reload UICollectionView again.
This will more easy as compared to adding buttons and setting frames.
And design UICollectionView cell in such way that it will same length as your text, use NSLayoutConstraints for it. It will work fast and smooth.
Code will be minimum and reusable.
Try out this working code...
Take UIScrollView in your Xib for buttons
//Adding menu buttons
btnArray = #[#"SYMPTOMATIC", #"SOCIAL", #"VOCATIONAL", #"PERSONAL"];
[self addButtonsInScrollMenu:btnArray];
/**
* Add Menu Buttons in Menu Scroll View
*
* #param buttonArray Array containing all menu button title
*/
#pragma mark - Add Menu Buttons in Menu Scroll View
-(void) addButtonsInScrollMenu:(NSArray *)buttonArray
{
CGFloat buttonHeight = self.menuScrollView.frame.size.height;
CGFloat cWidth = 0.0f;
for (int i = 0 ; i<buttonArray.count; i++)
{
NSString *tagTitle = [buttonArray objectAtIndex:i];
CGFloat buttonWidth = [self widthForMenuTitle:tagTitle buttonEdgeInsets:kDefaultEdgeInsets];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(cWidth, 0.0f, buttonWidth, buttonHeight);
[button setTitle:tagTitle forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:#"Roboto-Regular" size:12.0f];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self.menuScrollView addSubview:button];
UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, button.frame.size.height - kButtonBorderHeight, button.frame.size.width, kButtonBorderHeight)];
bottomView.backgroundColor = [UIColor darkTextColor];
bottomView.tag = 1001;
[button addSubview:bottomView];
if (i == 0)
{
button.selected = YES;
[bottomView setHidden:NO];
}
else
{
[bottomView setHidden:YES];
}
cWidth += buttonWidth;
}
NSLog(#"scroll menu width->%f",cWidth);
self.menuScrollView.contentSize = CGSizeMake(cWidth, self.menuScrollView.frame.size.height);
}

Select/Deselect for dynamically created UIButtons

I will explain the Scenario:
I am dynamically creating three UIButtons inside a for loop.The condition is by default the first button should be bordered.Now if we select other buttons the previous button's border should go and now the border should be present for selected button.
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
self.graphSubDataButton = [[UIButton alloc] init];
self.graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[self.graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[self.graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
self.graphSubDataButton.tag = btnsCount+1;
[self.graphSubDataButton addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
if (self.graphSubDataButton.tag==1)
{
[self.graphSubDataButton.layer setBorderWidth:1.2f];
[self.graphSubDataButton.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[self.graphSubDataButton.layer setCornerRadius:8.0f];
}
}
-(void) enableRespectiveButton2:(UIButton *) sender
{
}
I have changed your code structure little to make it work properly, you can comment if you don't understand something.
NSInteger defaultSelectedTag = 1;
for (int btnsCount = 0 ; btnsCount <[DataList count]; btnsCount++) {
UIButton *graphSubDataButton = [UIButton buttonWithType:UIButtonTypeCustom];
graphSubDataButton.frame = CGRectMake(buttonsOffset, 0, 120, 40);
[graphSubDataButton setTitleColor:UIColorFromRGB(0x548DCD) forState:UIControlStateNormal];
[graphSubDataButton.titleLabel setFont:[UIFont fontWithName:AVENIR_NEXT_MEDIUM size:13.0f]];
graphSubDataButton.tag = btnsCount+1;
[graphSubDataButton addTarget:self action:#selector(enableRespectiveButton:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + self.graphSubDataButton.frame.size.width ;
//add your button somewhere
//e.g.
//[self.view addSubview:graphSubDataButton];
if (graphSubDataButton.tag == defaultSelectedTag) {
[self setBorderForButton:graphSubDataButton];
}
}
- (void) setBorderForButton:(UIButton *)sender {
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[sender.layer setCornerRadius:8.0f];
}
- (void) enableRespectiveButton2:(UIButton *) sender {
for(UIButton *buttons in [sender.superview subviews]) {
if([buttons isKindOfClass:[UIButton class]]) {
if(![buttons isEqual:sender]) {
//add logic to, remove borders
//buttons.layer.borderWidth = 0.0f;
}
}
}
[self setBorderForButton:sender];
}
Use tag to get current selected button or much simple: iterate through all subviews and set border to 0 for UIButtons
for (UIView *subview in [uiv_ButtonsView subviews]) {
if([subview isKindOfClass:[UIButton class]]) {
subview.layer setBorderWidth:0.0f;
}
}
then use:
UIButton *button=(UIButton *)sender;
[button.layer setBorderWidth:1.2f];
[button.layer setBorderColor:[UIColorFromRGB(0x3070BA) CGColor]];
[button.layer setCornerRadius:8.0f];
Create dynamic buttons like :
int buttonsOffset = 50;
for (int btnsCount = 0 ; btnsCount <3; btnsCount++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor = [UIColor blueColor];
btn.frame = CGRectMake(buttonsOffset, 100, 120, 40);
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[btn.titleLabel setFont:[UIFont systemFontOfSize:13.0f]];
btn.tag = btnsCount+1;
[btn addTarget:self action:#selector(enableRespectiveButton2:) forControlEvents:UIControlEventTouchUpInside];
buttonsOffset = buttonsOffset+30 + btn.frame.size.width;
if (btn.tag==1)
{
[btn.layer setBorderWidth:1.2f];
[btn.layer setBorderColor:[UIColor greenColor].CGColor];
[btn.layer setCornerRadius:8.0f];
self.graphSubDataButton = btn;
}
[self.view addSubview:btn];
}
And click event of button is as following
- (void)enableRespectiveButton2:(UIButton *)sender
{
sender.selected = TRUE;
[sender.layer setBorderWidth:1.2f];
[sender.layer setBorderColor:[UIColor greenColor].CGColor];
[sender.layer setCornerRadius:8.0f];
self.graphSubDataButton.selected = FALSE;
[self.graphSubDataButton.layer setBorderWidth:0.0f];
[self.graphSubDataButton.layer setBorderColor:[UIColor clearColor].CGColor];
[self.graphSubDataButton.layer setCornerRadius:0.0f];
self.graphSubDataButton = sender;
}
Create dynamic buttons with for loop as shown below.
for(int i = 0 ;i <3; i++) {
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
NSString * title = [NSString stringWithFormat:#"Button %d",i];
[btn setTitle:title forState:UIControlStateNormal];
[btn setBackgroundImage:[UIImage imageNamed:#"BorderImg"] forState:UIControlStateSelected];
[btn setBackgroundImage:[UIImage imageNamed:#"UnBordered"] forState:UIControlStateNormal];
if(i == 0) [btn setSelected:YES];
else [btn setSelected:NO];
[btn setTag:i];
[btn addTarget:self action:#selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
}
Have 2 images
Highlighted border (BorderImg)
Normal border (UnBordered)
Set those images to button while creating in for loop
- (void)btnTapped:(UIButton *) btn {
if(! btn.isSelected) {
[btn setSelected:YES];
}
Inside button selector method, based on the sender selection state , update the button selected state. (In side selector method i.e btnTapped set other buttons selected state to false)

Programmatically Click 10 UIButtons one after another in UIScrollView

I am developing one control where 10 UIButtons are added in UIScrollView. Now I have a requirement to click every Button one after another after some delay. can you all guide me how to do that?
here is the code what I have done.
in viewcontroller.h file
#property (weak) IBOutlet UIScrollView *mapScrollView;
#property (strong) UIButton *addContactIcon;
in viewcontroller.m file
// Set up ScrollView with UIButtons
NSUInteger xPosition = 1;
NSUInteger countIndex = 0;
for (ContactModule *sortedContacts in _distanceList) {
_addContactIcon = [[UIButton alloc] initWithFrame:CGRectMake(xPosition, 7, 30, 30)];
_addContactIcon.tag = countIndex;
[_addContactIcon addTarget:self action:#selector(mapsScrollButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
_addContactIcon.layer.cornerRadius = 2.0;
_addContactIcon.clipsToBounds = YES;
[_addContactIcon setBackgroundImage:[UIImage imageWithContentsOfFile:dataPath] forState:UIControlStateNormal];
[_addContactIcon setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[_mapScrollView addSubview:_addContactIcon];
xPosition += _addContactIcon.frame.size.width + 2;
countIndex = countIndex + 1;
}
_mapScrollView.contentSize = CGSizeMake(30 * _distanceList.count + 34, 40);
[_mapScrollView setShowsHorizontalScrollIndicator:NO];
Here is the Button Click Method For Every Button:
- (void)mapsScrollButtonClicked:(UIButton *)clickButton {
// Set Annotation Callout
[_mapsView selectAnnotation:[_mapsView.annotations objectAtIndex:clickButton.tag] animated:YES];
}
Now the requirement is I want to call Action Method for Every UIButtons in UIScrollview. for that I am doing something wrong below. help me with that:
for(NSUInteger count=0; count<[_distanceList count];count++) {
[UIView animateWithDuration:0.4
delay:0.8
options:0
animations:^{
[_addContactIcon sendActionsForControlEvents:UIControlEventTouchUpInside];
} completion:nil];
}
Hi you could do soemthing like below to click all the UIButtons on your view.
UIButton *button;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *buttonMaster;
buttonMaster = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[buttonMaster addTarget:self
action:#selector(masterButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[buttonMaster setTitle:#"Show View" forState:UIControlStateNormal];
buttonMaster.frame = CGRectMake(80.0, y, 160.0, 40.0);
buttonMaster.tag = 100;
[self.view addSubview:buttonMaster];
y = 60;
for (int x=0; x<=10; x++)
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(button1:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, y, 160.0, 30.0);
button.tag = x;
//[paintView addSubview:button];
[self.view addSubview:button];
y = y + 70;
}
[buttonMaster sendActionsForControlEvents:UIControlEventTouchUpInside];
if ([[self.view viewWithTag:0] isKindOfClass:[UIButton class]]) {
//UIButton *button = (UIButton *) view;
// do something funky with button
UIButton *myBtns = (UIButton *)[self.view viewWithTag:0];
NSLog(#" button tapped is %ld", myBtns.tag);
[myBtns sendActionsForControlEvents:UIControlEventTouchUpInside];
[myBtns setBackgroundColor:[UIColor greenColor]];
} else {
// do something funky with view
NSLog(#"do nothing ");
}
}
- (void)listSubviewsOfView:(UIView *)view {
// Get the subviews of the view
NSArray *subviews = [view subviews];
// Return if there are no subviews
//if ([subviews count] == 0) return; // COUNT CHECK LINE
for (UIView *subview in subviews) {
UIButton *myBtns = (UIButton *)[self.view viewWithTag:subview.tag];
//UIButton *myBtns = (UIButton *)[self.view viewWithTag:0];
if(myBtns.tag == 100 ){
NSLog(#"Master Button tag :- Dont do anything ");
}else
{
//[myBtns sendActionsForControlEvents:UIControlEventTouchUpInside];
NSLog(#"tags are %ld ", myBtns.tag);
}
// List the subviews of subview
//[self listSubviewsOfView:subview];
}
}
-(void) clickAllButtons{
for (int i = 0; i<=10; i++) {
UIButton *myBtns = (UIButton *)[self.view viewWithTag:i];
[myBtns sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
- (IBAction)masterButtonTapped:(UIButton*)sender{
//[self listSubviewsOfView:self.view];
// [self clickAllButtons];
}
- (IBAction)button1:(UIButton*)sender{
NSLog(#"test");
//NSLog(#"Button clicked %ld", tag);
}

Dealing with a position for a single UIButton in a custom UIAlertView

I've a nice function for customizing UIAlertView, it works perfect with 2 buttons but the issue is, how do I know there is only 1 button and not 2 buttons?
Or how do I solve the problematic positioning with only 1 button?
My code is:
- (void)layoutSubviews
{
for (id obj in self.subviews) //Search in all sub views
{
if ([obj isKindOfClass:[UIImageView class]])
{
UIImageView *imageView = (UIImageView*)obj;
float width = 284.0;
float height = 141.0;
CGSize size = CGSizeZero;
size.width = width;
size.height = height;
UIImage *newImage = [self imageWithImage:[UIImage imageNamed:#"CustomAlertView"] scaledToSize:size];
imageView.image = newImage;
[imageView sizeToFit];
}
if ([obj isKindOfClass:[UILabel class]]) // Override UILabel (Title, Text)
{
UILabel *label = (UILabel*)obj;
label.backgroundColor = [UIColor clearColor];
label.textColor = [UIColor blackColor];
label.shadowColor = [UIColor clearColor];
label.font = [UIFont fontWithName:kFONT_NAME size:kFONT_SIZE];
}
if ([obj isKindOfClass:[UIButton class]]) // Override the UIButton
{
UIButton *button = (UIButton*)obj;
CGRect buttonFrame = button.frame; // Change UIButton size
buttonFrame.size = CGSizeMake(127, 35);
CGFloat newYPos = buttonFrame.origin.y + 9; // Change UIButton position
buttonFrame.origin.y = newYPos;
button.frame = buttonFrame;
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
switch (button.tag)
{
case kFIRST_BUTTON:
{
[button setBackgroundImage:[UIImage imageNamed:#"short_orange_button_off"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"short_orange_button_on"] forState:UIControlStateHighlighted];
}
break;
case kSECOND_BUTTON:
{
[button setBackgroundImage:[UIImage imageNamed:#"short_button_black_off"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"short_button_black_on"] forState:UIControlStateHighlighted];
}
break;
}
}
}
[self updateConstraints];
}
OK, just figure it out by myself, I added:
// Determine how many buttons we deal with
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
for (id obj in self.subviews)
{
if ([obj isKindOfClass:[UIButton class]])
{
self.numOfButtons++;
}
}
NSLog(#"self.numOfButtons: %d", self.numOfButtons);
});
Right after:
- (void)layoutSubviews
Then I know how many buttons I deal with.

UIButton font not smooth

I add some UIButton to the view, but the font in the button seems not smooth(in retina screen)....
See the image:
anyone help? Thanks!
Here is the method I adding the buttons:
- (void)setItems:(NSArray *)items {
if (_items != items) {
_items = items;
for (UIView *v in self.containerView.subviews) {
[v removeFromSuperview];
}
int i = 1;
CGFloat y = 8;
for (id item in _items) {
UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
itemButton.frame = CGRectMake(0, y, self.containerView.bounds.size.width, 35);
itemButton.tag = i;
[itemButton setTitle:item forState:UIControlStateNormal];
[itemButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[itemButton setBackgroundImage:[UIImage imageNamed:#"bg.png"] forState:UIControlStateHighlighted];
itemButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
[itemButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
y += 35;
[self.containerView addSubview:itemButton];
i++;
}
}
}
The problem resolved:
self.containerView = [[UIScrollView alloc] initWithFrame:frame];
UIScreen *mainScreen = [UIScreen mainScreen];
if ([mainScreen respondsToSelector:#selector(scale)]) {
self.containerView.contentScaleFactor = mainScreen.scale;
}
self.containerView.contentScaleFactor = mainScreen.scale;

Resources