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 *)];
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'm having a weird problem, which I'm sure has an easy fix/explanation, but I've spent the weekend trying without luck.
I have a custom UIView as my tableHeaderView, which contains two UIButtons. Pressing one UIButton or the other, changes the data inside the UITableView by changing the dataSource, and doing a [tableView reloadData].
The problem I have is when after pressing one of the buttons, I reload the table but I have no rows (dataSource/table is empty). I can't press the other button to change the dataSource and reload the table back because It becomes unresponsive, doesn't respond to touches.
The only explanation I have is maybe UITableViews don't respond to touches when they are empty? But seems like a bug that the tableHeaderView doesn't respond either..
This happens both in iOS 6 and iOS 7.
PS: I'm aware I could detect the dataSource is empty before doing a reloadData and keep the data for the other dataSource, but I need to show the button as pressed and show a message under it when is empty, so that doesn't help me.
This is how I create the Table header view, and assign it to the tableView.
self.tableCustomHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 48)];
self.tableCustomHeader.backgroundColor = [UIColor whiteColor];
// Configure Header
self.rankingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.rankingBtn.frame = CGRectMake(3, 4, 157, 40);
self.rankingBtn.adjustsImageWhenHighlighted = NO;
self.rankingBtn.adjustsImageWhenDisabled = NO;
[self.rankingBtn setImage:[UIImage imageNamed:#"amigos_boton_ranking_on.png"] forState:UIControlStateNormal];
[self.rankingBtn setImage:[UIImage imageNamed:#"amigos_boton_ranking_off.png"] forState:UIControlStateDisabled];
[self.rankingBtn setImage:[UIImage imageNamed:#"amigos_boton_ranking_off.png"] forState:UIControlStateHighlighted];
[self.rankingBtn addTarget:self action:#selector(fetchRanking:) forControlEvents:UIControlEventTouchUpInside];
if (self.listType == FriendsListTypeRanking) {
self.alfabeticBtn.enabled = NO;
}
self.alfabeticBtn = [UIButton buttonWithType:UIButtonTypeCustom];
self.alfabeticBtn.frame = CGRectMake(160, 4, 157, 40);
self.alfabeticBtn.adjustsImageWhenHighlighted = NO;
self.alfabeticBtn.adjustsImageWhenDisabled = NO;
[self.alfabeticBtn setImage:[UIImage imageNamed:#"amigos_boton_alfabetico_on.png"] forState:UIControlStateNormal];
[self.alfabeticBtn setImage:[UIImage imageNamed:#"amigos_boton_alfabetico_off.png"] forState:UIControlStateDisabled];
[self.alfabeticBtn setImage:[UIImage imageNamed:#"amigos_boton_alfabetico_off.png"] forState:UIControlStateHighlighted];
[self.alfabeticBtn addTarget:self action:#selector(fetchFriends:) forControlEvents:UIControlEventTouchUpInside];
if (self.listType == FriendsListTypeAlfabetic) {
self.alfabeticBtn.enabled = NO;
}
[self.tableCustomHeader addSubview:self.alfabeticBtn];
[self.tableCustomHeader addSubview:self.rankingBtn];
[self.table setTableHeaderView:self.tableCustomHeader];
After that, there is not much else to show. If I return 0 for numberOfRowsInSection, I can no longer press the other UIButton inside the tableHeaderView to change to the other set of data.
Without seeing the implementation of the selectors theres still no way to figure out whats wrong with the app. Though one problem that looks to be easily solved is the fact that you're always setting the alfabeticBtn.enabled to NO.
See:
if (self.listType == FriendsListTypeRanking) {
self.alfabeticBtn.enabled = NO;
}
if (self.listType == FriendsListTypeAlfabetic) {
self.alfabeticBtn.enabled = NO;
}
I would fix that and then if you're still having problems update your question with the selector methods.
I present modal view controllers, packaged in a UINavigationController.
When they first appear, the bar button items are alright.
However, if I click a button and a new view controller is pushed to the UINavigationController, and then I use the back button of that view controller, and it gets popped back to the original modal view controller, the buttons do not show up, (but they work when I click them -> just can't see them)
I can't seem to figure out why this occurs.
Here's some code.
Oh, and by the way, I customize the navigation controller's navigation bar's background. I have a feeling this may be causing this visual interference, but I don't know what to change to get it right.
In GGMainViewController:
GGSelectTeamsViewController *chooseTeam = [[GIFSelectTeamsViewController alloc] init];
UINavigationController* navigationController = [[UINavigationController alloc] initWithRootViewController:chooseTeam];
[self setupModalNavigationBar:navigationController];
[self presentViewController:navigationController animated:YES completion:nil];
- (void)setupModalNavigationBar:(UINavigationController *)nav {
// unnecessary code removed for a quicker read
// basically navigation bar has a background gradient as well as a bottom border
[nav.navigationBar.layer addSublayer:bottomBorder];
[nav.navigationBar.layer addSublayer:gradient];
}
In GGSelectTeamsViewController
- (void)viewDidLoad
{
[super viewDidLoad];
title = [[UILabel alloc] init];
title.text = someText;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor whiteColor];
self.navigationItem.titleView = title;
[title sizeToFit];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
backButton.frame = someFrame;
[backButton addTarget:self action:#selector(goBack) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton] animated:NO];
UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeCustom];
[selectButton setTitle:#"Select" forState:UIControlStateNormal];
[selectButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
selectButton.frame = someFrame;
[selectButton addTarget:self action:#selector(sendContent) forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:selectButton] animated:NO];
}
Since the navigation items are set in the viewDidLoad method, why can't I see them (but they are still functional) when I return to it from another view controller?
That seems like an over-complicated way of doing this.
Why don't you create your own button and then use the setRightBarButtonItems:items: method from the UINavigationBar to set the buttons you want?
I'm guessing they disappear because you're adding them to the layer of the UINavigationBar.
Still, if you want to keep this method with the layers, you may want to add them in the viewWillAppear method, but you may need call the removeFromSuperlayer to make sure you don't add stuff more than once.
Hope this helps.
The problem was:
the gradients for the navigation bar (done by code)
The solution was:
use Sketch to create an image of the gradients and use the appearance proxy to set the image as a background image
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:#"NavigationBarBackground"] forBarMetrics:UIBarMetricsDefault];
I want to implement this fade in/out in a horizontal scroll I have, but I can't figure out how to access my button that is in a subview of my UIScrollView. This is what I'm trying to implement Fade in/out stackoverflow.
This is my code...
Game *game = (Game *)[_schedule.games objectAtIndex:i];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, 20, 150, 100)];
[button setBackgroundImage:[UIImage imageNamed:game.opponent] forState:UIControlStateNormal];
//[button setTitle:game.opponent forState:UIControlStateNormal];
[_gameScrollList addSubview:button];
How Can I add the observer to my button, Can this be done?
[self.scrollView addObserver:[self.contentViews objectAtIndex:i]
forKeyPath:#"contentOffset"
options:NSKeyValueObservingOptionNew
context:#selector(updateAlpha:)];
To access the reference of this button added as subview, you can:
Use this button as a iVar, saving the reference to use it later;
Similarly, use this button as a property;
Iterate through subviews of _gameScrollList, looking for a subview of UIButton's class (not recommended, will work well only if it has just one UIButton as subview)
Example of how implement the second approach. Just declare a property:
#property(nonatomic, strong) UIButton *button;
and then:
Game *game = (Game *)[_schedule.games objectAtIndex:i];
self.button = [[UIButton alloc] initWithFrame:CGRectMake(x, 20, 150, 100)];
[self.button setBackgroundImage:[UIImage imageNamed:game.opponent] forState:UIControlStateNormal];
//[self.button setTitle:game.opponent forState:UIControlStateNormal];
[_gameScrollList addSubview:self.button];
So, after, when you want to access this button somewhere, just call for the property
self.button.alpha = 0.5; //example of interation with your button
If this is not what you are looking for, you really should edit your question to be more clear.
Hope it helped.
I want to display a UIButton in the header section of a UITableView. This is the code I have tried
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 285, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 320.0, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:#"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerView addSubview:headerViewButton];
The problem is I am having a hard time adjusting the button frame, so that it fits fine in the landscape mode as well as portrait mode. I am not very comfortable with using Interface Builder. How can I adjust the button frame correctly so that it fits the view.
I have tried using setAutoresizingMask
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0,0, 320, 44)];
UIButton *headerViewButton = [UIButton buttonWithType:UIButtonTypeCustom];
headerViewButton.frame = CGRectMake(headerView.bounds.size.width * 7/5, headerView.bounds.size.height * 1/4, 290, 30.0);
headerViewButton.backgroundColor = [UIColor grayColor];
[headerViewButton setTitle:#"Import main list from other field >" forState:UIControlStateNormal];
[headerViewButton addTarget:self
action:#selector(buttonPressed:)
forControlEvents:UIControlEventTouchDown];
[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin|UIViewAutoresizingFlexibleWidth];
[headerView addSubview:headerViewButton];
As the app loads for the first time, the button is missing, but as I change the orientation, the button appears. Every time I try, its the same pattern. The first time the app loads, the button would be missing, and it appears on both orientations after that.
You need to set the button's autoresizingMask to appropriate values. The values depend on how you want the button's size and/or position to adjust as its parent view's size changes.
In the code you posted, it is odd that you are making the button wider than its parent view.
Try setting
[headerViewButton setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
This way it will be resized as the superview changes. (Works the same as setting autoresizing options in IB)