I've seen almost every question about similar issues, and tried the given answers, but they are not working for me. The thing is: I have a home view, and I have a button, on click, it adds a new button and a label over it, and so on until you "fill" the area, and I want it to be resizable and scrollable, so you can see all the buttons and you can zoom-in to see just some buttons. I don't know if I'm explaining myself clearly...
So, I tried adding everything to a UIView, and it didn't work, I tried adding everything to a UIScrollView, and again it didn't work. I don't know what else to do. I've been dealing with this almost all week without any success. I have to say I am not very experienced on this, I just started to code for iOS about 4 months ago, so please be patient. I would really appreciate any help or guidance you can share.
Here's what I am doing:
- (IBAction) doneAdding:(id) sender{
boxes = boxes+1;
UIButton *newBox = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *boxImage = [UIImage imageNamed:#"bluecube.jpg"];
if (boxes <= 4) {
switch (boxes) {
case 1:
newBox.frame = CGRectMake(519, 356, 162, 163);
break;
case 2:
newBox.frame = CGRectMake(681, 519, 162, 163);
break;
case 3:
newBox.frame = CGRectMake(357, 519, 162, 163);
break;
case 4:
newBox.frame = CGRectMake(519, 844, 162, 163);
break;
default:
break;
}
[newBox setBackgroundImage:boxImage forState:UIControlStateNormal];
[newBox addTarget:self action:#selector(goToProject:) forControlEvents:UIControlEventTouchUpInside];
[homeView addSubview:newBox];
//I get the text of the label from a textfield
UILabel *nameLabel= [ [UILabel alloc ] initWithFrame:CGRectMake(480.0,500.0, 150.0, 43.0) ];
[[NSUserDefaults standardUserDefaults] setObject: newName.text forKey: #"SomeRandomText"];
nameLabel.textAlignment = UITextAlignmentCenter;
nameLabel.textColor = [UIColor blackColor];
nameLabel.backgroundColor = [UIColor clearColor];
nameLabel.font = [UIFont fontWithName:#"Helvetica" size:(12.0)];
nameLabel.text = [NSString stringWithFormat:#"%#", newName.text];
[homeView addSubview:nameLabel];
[newName release];
}else {
NSLog(#"No more boxes allowed");
}
}
What do you mean with "it doesn't work"? Did you try setting the UIScrollView's contentSize property to a size large enough to contain all the subview's frames?
Related
I have an issue in the iOS 8 with the UILabel. I am printing a text on the view while the text won't remove from the view if the result1 is bigger than the result2 while it is working fine in the iOS 7. And I have debugged and found that the label1.text is always null before I remove it from the screen. Please where would be my issue?
-(void)warningAlert{
label1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 85, 300, 660)];
label1.backgroundColor = [UIColor clearColor];
label1.textColor=[UIColor redColor];
label1.numberOfLines=0;
[self.view addSubview:label1];
if (result1 < result2)
{
if (printed == NO) {
label1.text = #"“Warning Warning!!”";
printed = YES;
}
}
else{
NSLog(#"check the value %#", label1.text);
[label1 removeFromSuperview];
printed = NO;
}
Instead of adding and removing the label.
Alloc the memory to label only once in viewDidlload
And then Just hide or unhide the label.
Let's say I have 5 pieces of text. I want to use a UITextView or UILabel to display it. I have a Next and Previous button to help me cycle it through. What's the best way to solve this problem?
NSString *text1 = #"Hello World 1"
NSString *text2 = #"Hello World 2"
NSString *text3 = #"Hello World 3"
NSString *text4 = #"Hello World 4"
NSString *text5 = #"Hello World 5"
this solution might be good
in .h file
UIButton *nextButton;
UIButton *backButton;
UILabel *textLabel;
NSArray *textStr;
int counter;
in .m file
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
counter=0;
textStr = [[NSArray alloc] initWithObjects:#"Today is rainy", #"Today is sunnt", #"Today is bright", #"Today is gloomy",
#"Today is beautifyl", nil];
textLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 200, 200)];
textLabel.text= [NSString stringWithFormat:#"%#", [textStr objectAtIndex:0]];
[self.view addSubview:textLabel];
nextButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[nextButton addTarget:self
action:#selector(btnClicked:)
forControlEvents:UIControlEventTouchDown];
nextButton.tag=1;
[nextButton setTitle:#"Next" forState:UIControlStateNormal];
nextButton.frame = CGRectMake(120.0, 150, 80, 40.0);
[self.view addSubview:nextButton];
backButton= [UIButton buttonWithType:UIButtonTypeRoundedRect];
[backButton addTarget:self
action:#selector(btnClicked:)
forControlEvents:UIControlEventTouchDown];
backButton.tag=2;
[backButton setTitle:#"Previous" forState:UIControlStateNormal];
backButton.frame = CGRectMake(30.0, 150, 80.0, 40.0);
[self.view addSubview:backButton];
}
-(void)btnClicked:(UIButton*)btn{
if (btn.tag==1) {
NSLog(#"%i", [textStr count]);
if (counter<[textStr count]-1) {
counter++;
NSLog(#"%i", counter);
textLabel.text= [NSString stringWithFormat:#"%#", [textStr objectAtIndex:counter]];
}
}
else{
if (counter>1) {
counter--;
textLabel.text= [NSString stringWithFormat:#"%#", [textStr objectAtIndex:counter]];
}
}
}
Here is the basic idea, as the implementation is trivial I won't post the exact code as it will beneficial to learn on your own.
1.Add all your strings to an NSArray.
2.Add two buttons to your view with InterfaceBuilder and link to your code.
3.Add a label to your view and link to your code.
4.Create an int property and call it counter.
5.When the users presses "next" we want to:
5.1 Increase the counter by 1.
5.2 Check to make sure the counter is higher than our array length.
5.3 If counter > array length then we can set it back to 0 to it loops around.
5.4 if counter <= array length then we do nothing.
5.5 grab the string out of the array at the counter index
5.6 set our label we created in #3 text to the retrieved string.
6.When the user presses "previous" we want to:
6.1 Decrease the counter by 1.
6.2 Check to make sure the counter is >= 0
6.3 If counter < 0 then we can set it equal to our array length so it loops
6.4 if counter <= 0 length then we do nothing.
6.5 grab the string out of the array at the counter index
6.6 set our label we created in #3 text to the retrieved string.
I am trying to implement ibooks bookshelf like functionality and I use GSBookShelf which is pretty simple and straight forward, it adds button as cells and it works great.
I want to add some labels to each button, I succeed but once in a while lets say 2 out of 5 times labels overlaps from previous buttons label.
normally like this
Sometime this happens:
Code:
- (UIView *)bookShelfView:(GSBookShelfView *)bookShelfView bookViewAtIndex:(NSInteger)index {
NSDictionary *currentArticle = [_bookArray objectAtIndex:index];
static NSString *identifier = #"bookView";
MyBookView *bookView = (MyBookView *)[bookShelfView dequeueReuseableBookViewWithIdentifier:identifier];
if (bookView == nil) {
bookView = [[MyBookView alloc] initWithFrame:CGRectZero];
bookView.reuseIdentifier = identifier;
[bookView addTarget:self action:#selector(bookViewClicked:) forControlEvents:UIControlEventTouchUpInside];
}
[bookView setIndex:index];
[bookView setSelected:[(NSNumber *)[_bookStatus objectAtIndex:index] intValue]];
//[bookView setFileName:[currentArticle objectForKey:#"name"] ];
//[bookView setFileName:[currentArticle objectForKey:#"name"] :index];
UIImage *image = nil;
image = [self returnCellImagefor:[currentArticle objectForKey:#"imagename"]];
[bookView setBackgroundImage:image forState:UIControlStateNormal];
UILabel *_fileLabel=nil;
_fileLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 180, 200, 46)];
_fileLabel.text=[currentArticle objectForKey:#"name"];
_fileLabel.textColor=[UIColor whiteColor];
_fileLabel.backgroundColor=[UIColor clearColor];
_fileLabel.font = [UIFont fontWithName:#"Gill Sans" size:16];
//_fileLabel.textAlignment=UITextAlignmentCenter;
_fileLabel.textAlignment = NSTextAlignmentCenter;
_fileLabel.numberOfLines=0;
_fileLabel.lineBreakMode = UILineBreakModeWordWrap;
[_fileLabel sizeToFit];
[bookView addSubview:_fileLabel];
return bookView;
}
Images are good they never overlap but label does it sometimes.
Any idea why this is happening?
Where did you put bookShelfView function at? Show your TableView code. I bet the problem you have is that when you scroll down in table bookShelfView is being called again. Your label is being created twice for two different values.
That's the only way you see that overlap happening, this is being initiated over and over again
_fileLabel =[[UILabel alloc] initWithFrame:CGRectMake(0, 180, 200, 46)];
I would check your TableView Code. I had asked a similar question before. (not exact). Check it out UITableView and UILabel repeating
In my iOS app, I have some animations that seem to be coming out of nowhere. They don't happen at first, only after using the app a little. Here is a Youtube video with a better description:
http://www.youtube.com/watch?v=whfG67EP6kk
I'm not really sure why the elements are animating the way they are (or at all). The code I'm calling is just a simple [view addSubView:viewname]. Any help as to how this is happening and how to prevent it would be great.
EDIT
Here is the code for the text that comes flying in from seemingly all directions:
-(void)displayPublicDataForEntry:(PFObject *)entry likes:(unsigned long)likesCount comments:(unsigned long)commentsCount
{
[indicator stopAnimating];
[indicator removeFromSuperview];
[zippingLabel removeFromSuperview];
entryStatus.text = #"This entry is public";
entryStatus.backgroundColor = [UIColor clearColor];
entryStatus.frame = CGRectMake(10, 3, 200, 20);
[shareView addSubview:entryStatus];
// Display a UILabel of number of likes
if (likesCount == 1)
numberOfLikes.text = [NSString stringWithFormat:#"%lu like", likesCount];
else
numberOfLikes.text = [NSString stringWithFormat:#"%lu likes", likesCount];
CGSize stringsize = [numberOfLikes.text sizeWithFont:[UIFont systemFontOfSize:14]];
numberOfLikes.frame = CGRectMake(10, 22, stringsize.width, 20);
numberOfLikes.backgroundColor = [UIColor clearColor];
[shareView addSubview:numberOfLikes];
// Display a UILabel of number of comments
if (commentsCount == 1)
numberOfComments.text = [NSString stringWithFormat:#" / %lu comment", commentsCount];
else
numberOfComments.text = [NSString stringWithFormat:#" / %lu comments", commentsCount];
numberOfComments.frame = CGRectMake(numberOfLikes.frame.size.width + 10, 22, 100, 20);
numberOfComments.backgroundColor = [UIColor clearColor];
[shareView addSubview:numberOfComments];
viewEntry.frame = CGRectMake(self.view.frame.size.width - 95, 12, 100, 40);
[viewEntry addTarget:self
action:#selector(viewPublicEntry)
forControlEvents:UIControlEventTouchUpInside];
[shareView addSubview:viewEntry];
}
There's nothing in there (at least I don't think there is) that would cause these strange animations.
I had this problem. The cause ended up being a call to begin animations without a call to commit. I somehow called [UIView beginAnimations:Nil context:nil] twice.
I need a fully transparent searchbar with cancel button. I have tried many solutions But I couldnt find a better solution yet. When I try to remove background color it shows scope bar. Can any one give me some source code for fully transperant Searchbar with can button.
Here's
addSearchbar.backgroundColor = [UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:0.5];
UITextField *sbTextField = (UITextField *)[self.addSearchbar.subviews lastObject];
for (UIView *subview in addSearchbar.subviews)
{
NSLog(#"%#",subview);
if ([subview isKindOfClass:[UITextField class]])
{
sbTextField = (UITextField *)subview;
UIImage *image = [UIImage imageNamed: #"06_magnifying_glass.png"];
UIImageView *iView = [[UIImageView alloc] initWithImage:image];
iView.frame = CGRectMake(0, 0, 24, 24);
sbTextField.leftView.frame = CGRectMake(0, 0, 24, 24);
sbTextField.leftView = iView;
[sbTextField.rightView removeFromSuperview];
CGFloat myWidth = 24.0f;
CGFloat myHeight = 24.0f;
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, myWidth, myHeight)];
[myButton setImage:[UIImage imageNamed:#"clear.png"] forState:UIControlStateNormal];
[myButton setImage:[UIImage imageNamed:#"clear.png"] forState:UIControlStateHighlighted];
[myButton addTarget:self action:#selector(doClear:) forControlEvents:UIControlEventTouchUpInside];
sbTextField.rightView = myButton;
sbTextField.rightViewMode = UITextFieldViewModeWhileEditing;
break;
}
if([subview isMemberOfClass:[UISegmentedControl class]])
{
UISegmentedControl *scopeBar=(UISegmentedControl *) subview;
scopeBar.tintColor = [UIColor clearColor];
}
}
[sbTextField removeFromSuperview];
[addSearchbar addSubview:sbTextField];
[addSearchbar setSearchFieldBackgroundImage:[UIImage imageNamed:#"SearchBar.png"] forState:UIControlStateNormal];
CGRect sbFrame = self.addSearchbar.frame;
// Set the default height of a textfield
sbFrame.size.height = 31;
/* 8 is the top padding for textfield inside searchbar
* You may need to add a variable to 8 according to your requirement.
*/
sbFrame.origin.y = 6+self.addSearchbar.frame.origin.y;
sbTextField.frame = sbFrame;
sbTextField.textColor = [UIColor lightGrayColor];
[sbTextField setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin];
Need a fully transparent searchbar with cancel button and clear button but without scopebar.
Thanks in advance
As of iOS 5.0 you can do the following:
UIImage *imgClear = [UIImage imageNamed:#"clear"];
[addSearchBar setImage:imgClear forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
Thats it. You might also want to repeat the line for the UIControlStateHighlighted state.
The last thing you should be doing is digging around inside the subviews of the search bar. It is guaranteed to break some day. Use the proper API to customize any control.
See that you set the UIControlHighlighted state image first and then the UIControlStateNormal state image or else you might face an issue wherein the clearIcon is not set in the highlighted state. (Not sure why this problem occurs.)
[_searchBar setImage:[UIImage imageNamed:#"ClearIcon"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateHighlighted];
[_searchBar setImage:[UIImage imageNamed:#"ClearIcon"] forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
For those of you trying this in Swift (I know someone is going to come here looking for this...) here you go:
self.searchBar.setImage(UIImage(named: "ico-cancel"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Normal)
self.searchBar.setImage(UIImage(named: "ico-cancel"), forSearchBarIcon: UISearchBarIcon.Clear, state: UIControlState.Highlighted)
Make sure you have the correct image in your Assets.xcassets folder.
Here’s how you change the magnifying glass and clear button icon in a UISearchBar’s UITextField
Without New Icons:
// Reference search display controller search bar's text field (in my case).
UITextField *searchBarTextField = [self.searchDisplayController.searchBar valueForKey:#"_searchField"];
// Magnifying glass icon.
UIImageView *leftImageView = (UIImageView *)searchBarTextField.leftView;
leftImageView.image = [LeftImageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
leftImageView.tintColor = [UIColor whiteColor];
// Clear button
UIButton *clearButton = [searchBarTextField valueForKey:#"_clearButton"];
[clearButton setImage:[clearButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
clearButton.tintColor = [UIColor whiteColor];