I have created a UIView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
UIImage *image2 = [UIImage imageNamed:#"pinGreen_v1.png"];
UIImage *image1 = [UIImage imageNamed:#"PinDown1.png"];
UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 10, 40, 40)];
[btn1 setImage:image2 forState:UIControlStateNormal];
[self addSubview:btn1];
for(int i =1; i<20; i++){
UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(40 * i, 40, 40, 40)];
[btn2 setImage:image1 forState:UIControlStateNormal];
[btn2 addTarget:self
action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
}
return self;
}
With the above code I am drawing images in my view. I am loading this view from my view controller. I need to put this view inside a UIScrollview. Where should I create the scroll view? In the above view or the view controller where the above view is created.
The code inside my view controller is :
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, 320, 400)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[drawLines release];
I should be able to scroll through the images inside my view.
I would create the the scrollview in the viewcontroller and add the view to it.
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
DrawLineInMyClass *drawLines = [[DrawLineInMyClass alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
drawLines.backgroundColor = [UIColor blackColor];
[self.view addSubview:drawLines];
[scroll addSubview:drawLines];
scroll.contentSize = CGSizeMake(self.view.frame.size.width*drawLines.frame.size.width, self.view.frame.size.height);
[self.view addSubview:scroll];
[drawLines release];
[scroll release];
Related
i have an iPhone app with TabBarController that each tab has a Nav Controller.. one of the Nav Controller pushes to another Viewcontroller. in that View Controller i am trying to Hide the back button and place One of my custom buttons. in the
- (void)viewDidLoad
{
[super viewDidLoad];
self._allUsersParticipantsInTheVideo = [[NSMutableArray alloc] init];
self._allUsersInvitesInTheVideo = [[NSMutableArray alloc] init];
for (int i=0; i < [self._curVideoToShow._videoParticipants count]; i++) {
[self._allUsersParticipantsInTheVideo addObject:[self._curVideoToShow._videoParticipants objectAtIndex:i]];
}
for (int i=0; i < [self._curVideoToShow._videoInvitees count]; i++) {
[self._allUsersInvitesInTheVideo addObject:[self._curVideoToShow._videoInvitees objectAtIndex:i]];
}
[self setNavBar];
}
#pragma mark - Set NavBar
-(void)setNavBar{
self.navigationItem.hidesBackButton = YES;
UIView *navBarItemview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[navBarItemview setBackgroundColor:[UIColor clearColor]];
// Set cutsom back button
//UIImage *backImage = [UIImage imageNamed:#"backButton.png"];
UIImage *backImage = [UIImage imageNamed:#"LeftArrowBg.png"];
CGRect frame = CGRectMake(0, 6, backImage.size.width + 4, backImage.size.height);
UIButton *bttn = [[UIButton alloc] initWithFrame:frame];
[bttn setBackgroundImage:backImage forState:0];
[bttn setTag:kBackBttn];
[bttn setTitle:#"Back" forState:UIControlStateNormal];
[bttn setTitleEdgeInsets:UIEdgeInsetsMake(4, 5, 0, 0)];
[bttn.titleLabel setFont:[UIFont fontWithName:#"PTSans-Bold" size:12.0]];
[bttn setContentMode:UIViewContentModeScaleAspectFit];
[bttn addTarget:self action:#selector(backButtonPressed) forControlEvents:UIControlEventTouchUpInside];
[navBarItemview addSubview:bttn];
UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 4, 320, 40)];
[titleLbl setFont:[UIFont fontWithName:#"PTSans-Bold" size:24.0]];
[titleLbl setTextColor:[UIColor whiteColor]];
[titleLbl setTextAlignment:UITextAlignmentCenter];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTag:kTitleLbl];
[titleLbl setShadowOffset:CGSizeMake(0, 1)];
[titleLbl setShadowColor:[UIColor blackColor]];
[titleLbl setText:#"All Participants"];
[navBarItemview addSubview:titleLbl];
self.navigationItem.titleView = navBarItemview;
}
As you can see i've written :
self.navigationItem.hidesBackButton = YES;
but on the first time when the app is lunched, i see the back button of the NavBar only for the first time, but later on for every time that i open the app the button remains to be my custom one. ( and also when i Navigate it is my custom )
someone?
try with this..
-(void)viewWillAppear:(BOOL)animated
{
self.navigationItem.hidesBackButton = YES;
}
or
[self.navigationController.navigationItem setHidesBackButton:YES];
or
self.navigationController.navigationItem.backBarButtonItem = nil;
How can i code for UIButton to show only when user taps on UIScrollView for images.
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 10, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
Right now it is displaying on every image view but i want it should Show UIButton Done only when taps on the screen.
EDIT: If i add
[imageView addGestureRecognizer: tap];
and initiate Gesturerecognizer
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
[tap release];
then how can i code in handle tap method to show done button when user taps on image view
- (void)handleTap:(UIGestureRecognizer*)tap {
}
Thanks for help.
You need to add the gesture recognizer on the image view and not on the ViewController.
Also, you need to enable the user interaction for the image view like this:
imageView.userInteractionEnabled = YES;
Check UIScrollViewDelegate Method when you will scroll show your button will be shown and then if you want to hide that button hide it.
And for handleTap you set [imageView addGestureRecognizer:tap]; instead of [self addGestureRecognizer:tap];
create your button in ViewDidLoad and hide it initially and when user will tap image just set`
button.hidden = NO;
I have defined a uiscrollview and used a for loop to add buttons to it, but whenever I run the program, I come up with a blank screen.
Here is my viewDidLoad, this is all the code i have relating to this.
- (void)viewDidLoad
{
[super viewDidLoad];
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
names = [[NSMutableArray alloc]initWithObjects:#"Excavations",#"Concrete",#"Framing",#"Insulation",#"Drywall",#"Finish Carpentry",#"Paint",#"Electrical",#"HVAC",#"Plumbing",#"Roofing",#"Tile",#"Doors",#"Windows",#"Flooring",#"Countertops",#"Landscape",#"Hardscape", nil];
for (int i = 0; i < 18; i++) {
UIButton *button = [[UIButton alloc]init];
button.frame = CGRectMake(0, 50*i, 320, 50);
button.tag = i;
[button addTarget:self action:#selector(NewView:) forControlEvents:UIControlEventTouchUpInside];
button.titleLabel.text = [names objectAtIndex:i];
button.hidden= NO;
[scrollView addSubview:button];
}
// Do any additional setup after loading the view, typically from a nib.
}
You need to set up the type for button manually, or just initialize with it:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
Edit: you also forgot to add your scrollview to main view
[self.view addSubview:scrollView];
You have to add the UIScrollView to your view. Try
...
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 1000)];
scrollView.hidden = NO;
[self.view addSubview:scrollView];
...
I have created a unbutton programatically and made an action
{
UIButton *myButton1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton1.frame = CGRectMake(20, 20, 80, 44);
[myButton1 setTitle:#"Click Me!" forState:UIControlStateNormal];
[myButton1 addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton1];
}
and in IBAction i have a UIView as subview like this
- (IBAction)buttonClicked:(id)sender
{
CGRect myFrame = CGRectMake(0, 300, 100, 50);
UIView *myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myView];
[myView release];
}
What i now want is subview have to disappear when the user second time press myButton1. How can i do that?
Do some correction in our code -
**Action like this -**
- (IBAction)buttonClicked:(id)sender
{
UIView *myView;
if([self.view viewWithTag:56])
{
[[self.view viewWithTag:56] removeFromSuperview];
}
else
{
CGRect myFrame = CGRectMake(0, 300, 100, 50);
myView = [[UIView alloc] initWithFrame:myFrame];
myView.backgroundColor = [UIColor lightGrayColor];
[myView setTag:56];
[self.view addSubview:myView];
[myView release];
}
}
you can use [view removeFromSuperView] method to remove a view.
Change your buttonClicked: method to this.
- (void)buttonClicked:(id)sender
{
UIView *myView;
if(myView=[self.view viewWithTag:1234])
{
[myView removeFromSuperview];
}
else {
CGRect myFrame = CGRectMake(0, 300, 100, 50);
myView= [[UIView alloc] initWithFrame:myFrame];
[myView setTag:1234];
myView.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:myView];
[myView release];
}
[self.view setNeedsDisplay];
}
EDIT: It doesn't read well if you use IBAction and not use the Interface Builder.
In this code I want to do changes like as instead of button I want to create images in image view with scroll view with zoom in and zoom out event on image. And two or three button on every image view so that I can implement some event on button. How do that?
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 33;
[btnMenu setTag:0 ];
for (int i = 1; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
//awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
btnMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//NSData *data =UIImageJPEGRepresentation(, 1);
[btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]] forState:UIControlStateNormal];
CGRect frame = btnMenu.frame;
frame.size.width=320;
frame.size.height=460;
frame.origin.x=0;
frame.origin.y=0;
btnMenu.frame=frame;
[btnMenu setTag:i];
btnMenu.alpha = 1;
[btnMenu addTarget:self action:#selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside];
[awesomeView addSubview:btnMenu];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
-(IBAction)btnSelected:(id)sender{
UIButton *button = (UIButton *)sender;
int whichButton = button.tag;
NSLog(#"Current TAG: %i", whichButton);
if(whichButton==1)
{
first=[[FirstImage alloc]init];
[self.navigationController pushViewController:first animated:YES];
}
}
You need Image instead of Button right? Then Try This:
scroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
scroll.backgroundColor=[UIColor clearColor];
scroll.pagingEnabled=YES;
scroll.contentSize = CGSizeMake(320*33, 300);
CGFloat x=0;
for(int i=1;i<34;i++)
{
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(x+0, 0, 320, 460)];
[image setImage:[UIImage imageNamed:[NSString stringWithFormat:#"page-%d.jpg",i]]];
[scroll addSubview:image];
[image release];
x+=320;
}
[self.view addSubview:scroll];
scroll.showsHorizontalScrollIndicator=NO;
[scroll release];