newbie in ipad apps here n stucked on a very strange condition.
I have one view based application.
In view of Viewcontroller I have added two buttons to draw to shapes. One is cube and one is piramid. Shapes are drawn with openGL code. Both shapes have different view classes.
Now on clicking on the buttons of cube and pyramid respectively, shapes should be drawn into the view of viewController. I am successfully drawing cube shape but cannot draw pyramid shape..!!!
after trying different methods i came to conclusion that "self.view addSubView:tempView" is the problem. This code works fine with cube button , but not with pyramid. Cant fine the exact problem.
here is sample of code
buttons :
cubeButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(600, -5, 50, 50)];
[cubeButton addTarget:self action:#selector(cubeClicked:) forControlEvents:UIControlEventTouchUpInside];
[cubeButton setBackgroundImage:[UIImage imageNamed:#"square_3D.png"] forState:UIControlStateNormal];
pyramidButton = [[UIButton buttonWithType:UIButtonTypeCustom] initWithFrame:CGRectMake(700, -5, 50, 50)];
[pyramidButton addTarget:self action:#selector(piramidClicked:) forControlEvents:UIControlEventTouchUpInside];
[pyramidButton setBackgroundImage:[UIImage imageNamed:#"triangle_3D.png"] forState:UIControlStateNormal];
methods sample code:
-(IBAction) cubeClicked:(id)sender {
UIView *cubeView = [[CubeView alloc] initWithFrame:CGRectMake(250, 60, 500, 600)];
[self.view addSubview:cubeView];
}
-(IBAction) piramidClicked:(id)sender {
UIView *pyramidView = [[pyramidView alloc] initWithFrame:CGRectMake(250, 60, 500, 600)];
[self.view pyramidView];
}
Thanks in advance. I appreciate your help guys.
Is that a typo in your piramidClicked: function? If not, that could be the issue:
-(IBAction)piramidClicked:(id)sender {
UIView *pyramidView = [[PyramidView alloc] initWithFrame...];
// this line probably causes a compiler warning ...
// [self.view pyramidView];
[self.view addSubview:pyramidView];
}
If it is a typo, I'd check to make sure you made your connections correctly in Interface Builder. Hope that helps.
Related
I am rather new to Xcode and iOS development but have been enjoying the challenge so far.
I have run into an issue where I am attempting to move a UIImageView I have created programmatically on top of a MapBox mapView.
I would like to move this UIImageView with a UIButton by a measure of pixels, with the UIButton being on top of the mapView.
This is the code I have so far in my ViewController.m:
[self.view addSubview:mapView];
UIImageView* ship;
ship=[[UIImageView alloc] initWithFrame:CGRectMake(150, 200, 40, 40)];
UIImage * image;
image=[UIImage imageNamed:#"spaceShip"];
[ship setImage:image];
ship.alpha = 0.75;
[self.view addSubview:ship];
UIButton *upButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
upButton.frame = CGRectMake(150, 250, 40, 40);
upButton.userInteractionEnabled = YES;
[upButton setTitle:#"UP" forState:UIControlStateNormal];
[upButton addTarget:ship action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:upButton];
}
- (IBAction)moveUp {
ship.center = CGPointMake(ship.center.x, ship.center.y -10);
}
Could anyone show me how to get this upButton to recognize and move my UIImageView?
One problem is that ship is a local variable that you create in the first block of code. When that code block goes out of scope, ship will be nil, so when you try to set it's center in the button method, it won't work. You need to create a property for ship, and use that instead.
Your other problem is that when you add the action to your button, you call it buttonPressed:, but the method you implemented is moveUp. So, if you create a property called ship, then your action method should be,
- (void)buttonPressed:(UIButton *) sender {
self.ship.center = CGPointMake(self.ship.center.x, self.ship.center.y -10);
}
I am a newcomer to the realm of iOS development.
I am trying to get the UIScrollView control to work and ran across the following question:
steps for creating UIScrollView with Interface Builder
I followed the steps outlined in the answer for this question and everything works as I want. However, this appears to create a statically-sized view that is scrolled. What I am really after is a dynamically sized view that may or may not be scrolled. For example, instead of the view with buttons, I put a single label for which I set the text and number of lines in the viewDidLoad method. The view that contains the label is set to a static size so the scroll viewer does not attempt to scroll and the content of the label spills off of the page.
What am I missing?
you need to set te content size of the scroll
CGPoint controlsCenter = ViewBottom.center;
if(Pointsvalue > 5)
{
controlsCenter.y += txtFldFrame1.frame.origin.y+20;
ViewBottom.center = controlsCenter;
[ScrollLag addSubview:ViewBottom];
}
[ScrollLag setContentSize:(CGSizeMake(0, controlsCenter.y+100))];
make sure your label height changes on entering text.......
The URL in my comment to the original question provides a solution to the issue I raised.
I have a dynamically sized UIScrollView in a project I am working on right now. This is what I did:
self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 250, 748)];
[self.myScrollView setBackgroundColor:[UIColor blackColor]];
int heightCounter = 10;
UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
newButton.frame = CGRectMake(10, heightCounter, 200, 40);
[newButton setBackgroundColor:[UIColor clearColor]];
[newButton addTarget:self action:#selector(someMethod:) forControlEvents:UIControlEventTouchUpInside];
[newButton setTitle:#"ButtonTitle" forState:UIControlStateNormal];
[newButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.myScrollView addSubview:newButton];
heightCounter += 50;
// add additional items to the myScrollView and remember to increase the heightCounter
self.myScrollView.contentSize = CGSizeMake(250, heightCounter);
self.myScrollView.scrollEnabled = YES;
[self.view addSubview:self.myScrollView];
I create a UIButton and place it on top of the UIImageView which I place on UIScrollView. I add the button to the image like this:
backgroundImage.frame = CGRectMake(0, 0, backgroundImage.frame.size.width, mainTextView.frame.origin.y + mainTextView.frame.size.height + bottomMargin + attachBlockHeight);
[self insertSubview:backgroundImage atIndex:0];
UIButton *tmpButton = [[UIButton alloc] initWithFrame:CGRectMake(0, imgGreyLine.frame.origin.y + imgGreyLine.frame.size.height, backgroundImage.frame.size.width, attachBlockHeight)];
[tmpButton addTarget:self action:#selector(btnAttachClicked) forControlEvents:(UIControlEventTouchUpInside)];
//tmpButton.backgroundColor = [UIColor redColor];
[backgroundImage addSubview:tmpButton];
And the button works great on iOS6 and up - it's there, it receives touch events and does what it has to do.
However, on iOS5 I encounter an enormously strange behaviour - the frame of the button stays exactly the same and the button is clearly present on the view, but it only receives touch events when I slide my finger left or right on it. Moreover, there are no other UIViews on top of the button anywhere near!
What can be the reason for it? I'm totally confused in finding the bug here.
EDIT: It turns out that sliding UIButton works on iOS6 as well, but the thing is that the touch events are also registered.
You need to enable user interaction on the UIImageView parent
eg:
UIImageView *backgroundImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[backgroundImage setBackgroundColor:[UIColor yellowColor]];
[backgroundImage setUserInteractionEnabled:YES];
[self.view addSubview:backgroundImage];
UIButton *tmpButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,50,50)];
[tmpButton addTarget:self action:#selector(btnAttachClicked) forControlEvents:(UIControlEventTouchUpInside)];
tmpButton.backgroundColor = [UIColor redColor];
[backgroundImage addSubview:tmpButton];
You can not add UIbutton as a subview of UIImageview. Add it to the scrollview or the superview of UIimageview for that matter
I think the proper way to do what you want is to have a UIView with the imageview and button as siblings/children. make sure to disable user interaction on the imageview and you should be good.
I need a lot of custom buttons in my app. At the moment I'm using storyboard to define these buttons on every controller that I need. However, I feel that since I need them throughout my app, I'm better off putting in a different view controller/view that subclasses UIView or UIViewController so if I need to make any changes I will just have to do them once as opposed to in different view controllers. What would be the best way to do this? Also can you tell me how can I create buttons programatically? This is what I'm doing at the moment, and I'm getting a completely blank screen.
-(void)viewDidAppear:(BOOL)animated
{
UIButton *testButton = [UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(0, 390, 100, 40)];
[testButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[setImage:[UIImage imageNamed:#"test_button"] forState:UIControlStateNormal];
[setImage:[UIImage imageNamed:#"test_button"] forState:UIControlStateHighlighted];
[setEnabled:YES];
[self.view addSubview:testButton];
}
there is a chance that your button is way off the visible rect. for adding the button programmatically, you are doing it mostly right. also, images should have the file extension. since you are adding a customButton, if there are no images, then you wont see any button. Try adding a title to your button, which would show up even if there is no image added.
try the code below.
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]; //you must have a good reason to not add this line.
UIButton *testButton=[UIButton buttonWithType:UIButtonTypeCustom];
[testButton setFrame:CGRectMake(0, 20, 100, 40)];
[testButton setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin];
[testButton setImage:[UIImage imageNamed:#"test_button.png"] forState:UIControlStateNormal];
[testButton setImage:[UIImage imageNamed:#"test_button.png"] forState:UIControlStateHighlighted];
[testButton setEnabled:YES];
[self.view addSubview:testButton];
}
I've made a button on an overlay which loads every time the camera is accessed. The button responds fine and I've tested it to see whether it was a problem with the selector. I am 100% it's not a problem with the selector(it responded to an NSLog string).
I want to bring up a new view every time it is pressed, but it doesn't do anything even though I've used an assortment of methods. I'm running out of ideas here, I've tried many methods but none work, and I mean none. I made sure I've imported the frameworks right, I've even made new projects and redid everything from step one to make sure it wasn't a problem with the original project.
}
- (UIView*)CommomOverlay {
//Both of the 428 values stretch the overlay bottom to top for overlay function. It
doesn't cover it.
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,430)];
UIImageView *FrameImg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,430)];
[FrameImg setImage:[UIImage imageNamed:#"newGraphicOverlay.png"]];
FrameImg.userInteractionEnabled = YES;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 200, 44); // position in the parent view and set the
size of the button
[myButton setTitle:#"Click Me!" forState:UIControlStateNormal];
// add targets and actions
[myButton addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
[view addSubview:FrameImg];
[view addSubview:myButton];
return view;
}
The below does not work in my -(void)buttonPressed method:
UIViewController *viewControllerName = [[UIViewController alloc] initWithNibName:
(NSString *) bundle:(NSBundle *)];