Error with MKMapView - objective-c

When I create the story board below the map over takes the entire screen. This is understandble with the code that I have below it uses the entire bounds of the view. However, if I do a normal "init", I lose functionality of the map! The map usually zooms into my location, and a few other things. When I use init instead of initWithFrame the map doesn't go to my location.
self.myMapView =[[MKMapView alloc]initWithFrame:self.view.bounds];
self.myMapView.delegate=self;
How do I initialize the map without having it take over the entire screen like shown in my story board. New to iOS, thanks for your help!
//Setting up the location manger for finding the User's location
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
//Check to see if they have Authorization to use to use location servieces
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
//Initialize the map and specifiy bounds
self.myMapView =[[MKMapView alloc]initWithFrame:self.view.bounds];
//Set the delegate to itself
self.myMapView.delegate=self;
//Set a standard mapview, Enable scrolling and zooming
self.myMapView.mapType = MKMapTypeStandard;
self.myMapView.scrollEnabled = YES;
self.myMapView.zoomEnabled = YES;
//specifcy resizing
self.myMapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
//show the User's location and set tracking mode
self.myMapView.showsUserLocation = YES;
self.myMapView.userTrackingMode = MKUserTrackingModeFollow;
//add the VIEW!!
[self.view addSubview:self.myMapView];
//Set up the UISearch Bar
self.mySearch = [[UISearchBar alloc]init];
self.mySearch.delegate = self;
//Set the initial Text
self.mySearch.prompt = #"Enter Address or Use Current Location";

There's two approaches to create your map view.
1.Create in code. As shown in your example. You need to set the map view frame. If you need firstly to create UISearchBar, you should set map view frame according to search bar position. It might look like this.
self.mySearch = [[UISearchBar alloc] init];
self.mySearch.frame = CGRectMake(0, 0, self.view.frame.size.width, 40);
//...
self.myMapView = [[MKMapView alloc] init];
self.myMapView.frame = CGRectMake(0, self.mySearch.frame.size.height, self.view.frame.size.width, self.view.frame.size.height - self.mySearch.frame.size.height);
//...
2.Much more simpler way - create in storyboard. Firstly, there no need to create your outlets (such as search bar and map view) in code. You simple drag'n'drop all the controls to view. Open your storyboard, set the view controller class from UIViewController to your class name.
Note that the properties must be IBOutlets
#property (nonatomic,weak) IBOutlet MKMapView *myMapView;
#property (nonatomic,weak) IBOutlet UISearchBar *mySearchBar;
After dropping your controls to view, you must connect visual controls to your class outlets. Take a look at this tutorial where connections are explained.

Related

Only show the rows in UITableView

I have a UITableView and for the life of me I can't hide the header and all the footer. I have tried multiple methods posted on here.
I want to hide the white space on top and bottom of the populated rows.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//Disable Scrolling in TableView
self.tableView.scrollEnabled = NO;
//Get rid of unpopulated rows
self.tableView.tableFooterView = [[UIView alloc] init];
//Hide UITableViewHeader
self.tableView.tableHeaderView = [[UIView alloc] init];
//Populate Table with Test Data
[self testTableView];
}
It looks like your UITableView's layout frame is not getting properly adjusted. Since I am not sure if you are using autolayout constraints or autozingmask therefore please try adding following code to your view controller to force your table to size according to the screen.
- (void)viewDidLayoutSubviews {
self.tableView.frame = self.view.frame;
}
Just set the tableviewHeader to nil or if you want to hide section headers just return nil from viewForHeaderInSection and reload the tableData.

Subclassing UIActivityIndicator to change the Image

This is what I did:
#implementation BGUIActivityIndicator
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self customInitialize];
return self;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
[self customInitialize];
return self;
}
-(void)customInitialize
{
UIView * theImageView= [self findASubViewforClass:[UIImageView class]];//
UIImageView * theImageView1 = (UIImageView *) theImageView;
theImageView1.image = [UIImage imageNamed:#"spinner_blue"];
[theImageView1.image saveScreenshot];
while (false) ;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Everything seems perfect. [theImageView1.image saveScreenshot]; jot down both the old and new view perfectly.
However, nothing changes. Why?
I am not exactly asking how to change the image of UIActivityIndicator. There are tons of it already. I want to use it by subclassing UIActivityIndicator because I think it's the most elegant solution. I can't seem to do that.
In particular, I am asking why my approach, which works for changing background of search controller, for example, doesn't work for this.
According to the UIActivityIndicatorView Class Reference ,there is no way/ chance to change the image through sub-classing.
However you can change its activityIndicatorViewStyle , color of the activity indicator,UIActivityIndicatorStyle etc..
I think, without sub-classing, the class class UIImageView provides a very useful and simple way to implement such a thing. The only thing you have to do is to:
1.Provide a number of images that reflect your indicator animation.
2.Create a new UIImageView instance and set images and animation duration.
3.Position your custom activity indicator within your current view.
SAMPLE CODE:
//Create the first status image and the indicator view
UIImage *statusImage = [UIImage imageNamed:#"status1.png"];
UIImageView *activityImageView = [[UIImageView alloc]
initWithImage:statusImage];
//Add more images which will be used for the animation
activityImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"status1.png"],
[UIImage imageNamed:#"status2.png"],
[UIImage imageNamed:#"status3.png"],
[UIImage imageNamed:#"status4.png"],
[UIImage imageNamed:#"status5.png"],
[UIImage imageNamed:#"status6.png"],
[UIImage imageNamed:#"status7.png"],
[UIImage imageNamed:#"status8.png"],
nil];
//Set the duration of the animation (play with it
//until it looks nice for you)
activityImageView.animationDuration = 0.8;
//Position the activity image view somewhere in
//the middle of your current view
activityImageView.frame = CGRectMake(
self.view.frame.size.width/2
-statusImage.size.width/2,
self.view.frame.size.height/2
-statusImage.size.height/2,
statusImage.size.width,
statusImage.size.height);
//Start the animation
[activityImageView startAnimating];
//Add your custom activity indicator to your current view
[self.view addSubview:activityImageView];
See the full details Here

Displaying map with Objective C

I am using this code for creating this map:
AGSTiledMapServiceLayer *tiledLayer = [AGSTiledMapServiceLayer
tiledMapServiceLayerWithURL:[NSURL URLWithString:#"http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"]]; [self.mapView addMapLayer:tiledLayer withName:#"Tiled Layer"];
[self addSubView:mapView];
return self;
I am returning the self and want to display it like this:
Mymap *myMap = [[Mymap alloc] initWithFrame:CGRectMake(20,20, 100, 100)];
[self.webView addSubView:myMap];
Nothing happens...any ideas?
Use storyboard or xib. Then, add uiview on which add mapview. Now, connect this mapOutlet to the specific class by control drag. And, that's it, it will show you the map on your device.

Subview of TableView removed - Tableview not visible

For the first time working with CorePlot (after a couple of hours trying to set it up :P )
on my view, i have a tableview. when a certain IBAction is called i want to show another view (a graph) instead of the tableview.
my approach was to add a subview with the same size to the tableview. it works fine to display the graph, but when i remove the graphs view from [table subviews] the tableview does not reappear.
note:
expenseTable: my tableView
hasSubView: (BOOL) that indicates if a graph is shown right now or not
code
-(IBAction)displayDayBalanceGraph:(id)sender{
if (hasSubView) {
[[expenseTable subviews] makeObjectsPerformSelector: #selector(removeFromSuperview)];
NSLog(#"%#",expenseTable.subviews);
}
else{
[self initializeMonthArray];
CPTGraphHostingView *host = [self buildGraphView];
[expenseTable addSubview:host];
CPTXYGraph *graph = [[CPTXYGraph alloc ]initWithFrame:host.frame];
host.hostedGraph = graph;
CPTScatterPlot *plot = [[CPTScatterPlot alloc]init ];
plot.dataSource = self;
[graph addPlot:plot];
[expenseTable reloadData];
hasSubView = !hasSubView;
}
}
-(CPTGraphHostingView *)buildGraphView{
CPTGraphHostingView *view = [[CPTGraphHostingView alloc]initWithFrame:CGRectMake(0, 0, 312, 260)];
[view setBackgroundColor:[self grayColor]];
return view;
}
1st Screenshot: TableView displayed
2nd Screenshot: GraphView displayed
sidenote: this is a sampleplot =)
3rd Screenshot: GraphView dismissed
has anyone an idea what i missed? (or messed ;) )
It's not generally a good idea to add views as subviews of UITableView.
Instead, you could either remove the table view and replace it with the Core Plot view:
[tableView removeFromSuperview];
[containerView addSubview:corePlotView];
Make sure you have a reference to the table view somewhere or it will be released.

Adding UISearchBar programatically - Bar is Off Screen at Launch - iPad

I have added a UISearchBar to a table in my iPad app using the below code. The table is on the left hand side of my split view controller.
The problem is that when the app starts the search bar is strangely off screen to the left - you can just see the last few pixels of it. If you click on it, or scroll down and back up, it reverts itself and looks like it should.
Can anyone suggest how to fix this ?
Thanks
searchBar = [[UISearchBar alloc] init];
self.tableView.tableHeaderView = searchBar;
searchController = [[UISearchDisplayController alloc]
initWithSearchBar:searchBar
contentsController:self];
searchBar.delegate = self;
searchController.delegate = self;
searchController.searchResultsDelegate=self;
searchController.searchResultsDataSource=self;
You can try to create the UISearchBar with a frame like:
CGRect searchViewFrame = CGRectMake(33, 33, 264, 31);
or whatever dimensions or origin.
Like other resource, you can create the UISearchBar within a UIView, and add this UIView to the UITableView:
UIView *containerSearch = [[UIView alloc] initWithFrame: searchViewFrame];
searchBar = [[UISearchBar alloc] init];
[containerSearch addSubview: searchBar];
self.tableView.tableHeaderView = containerSearch;