how to change the height of two views (One view shrink and the other expand) using objective c? - objective-c

i have two views(A&B) in the same view controller(popOver window) and want to toggle between them, view A is larger than view B, when the popOver window first loads i want view A only to appear and when i press certain button inside it , it should shrink down to let view B appear above it, i tried to set the height of view B to fit into the all window when the window first load(Inside viewDidLoad)then reduce it's height and increase the height of the other view when a button is clicked, i try that code but it didn't change the height of the view !!
-(void)viewDidLoad
{
[super viewDidLoad];
UIView *contactsView=[self.view viewWithTag:2];// is this a correct initialization of a uiView from a view in a storyBoard ?
CGRect frame=contactsView.frame;
frame.size.height+=100;
contactsView.frame=frame;
}
any idea about that will be helpful,thanks.

As a general answer (I don't have time for more detail), I would use 3 views overall:
1) The view owned by your View Controller.
2) View A
3) View B
View A and B can be created in viewDidLoad of your view controller. Then set the frame of each of these for the position and size. Add them both to the view:
// in your .h file create these variable:
UIView* m_vA;
UIView* m_vB;
- (void)viewDidLoad
{
m_vA = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50.0, 50.0)] autorelease];
m_vB = [[[UIView alloc] initWithFrame:<your rect for View B>] autorelease];
// The views will automatically be retained.
[self.view addSubview:vA];
[self.view addSubview:vB];
m_vB.hidden = YES;
}
// In your button callback, toggle the visibility of views
- (IBAction)buttonPressed:(id)sender
{
// Toggle the visibility of views
m_vA.hidden = !m_vA.hidden;
m_vB.hidden = !m_vB.hidden;
}
// Dont forget to clean up the memory of these views in dealloc and viewDidUnload.

Related

Programmatically drawn UIView tags (or something alike)

I'm drawing a view that contains an xib, multiple times (same uiview) and then update the outlets each time it gets drawn displaying an nsmutablearray with different strings on the outlets
Clicking the view opens a viewcontroller that should be displaying the data from the view (+more), this works perfectly fine except it doesn't know which index of the array it's supposed to be displaying.
I'm trying to figure out a way to manage them in a way that I can see which view is being pressed so I can pass that 'id' onto the new vc (something to uniquely identify the view even though it's the same view being drawn)
Here's how the view is being drawn multiple times
- (void) populateUpcoming:(int)events {
[self resetVariables];
upcomingEventsCenterPos = self.view.frame.size.width / 2 - 159;
for (int i = 0; i < events; i++) {
upcomingEventsY2 = 175 * upcomingEvents2;
UIView *firstViewUIView = [[[NSBundle mainBundle] loadNibNamed:#"UpcomingEventFull" owner:self options:nil] firstObject];
[_scrollView addSubview:firstViewUIView];
CGRect frame = firstViewUIView.frame;
frame.origin.y = 9 + upcomingEventsY2;
frame.origin.x = upcomingEventsCenterPos + upcomingEventsX2;
firstViewUIView.frame= frame;
upcomingEvents2++;
UITapGestureRecognizer *singleFingerTap =
[[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tapUpcomingEvent:)];
[firstViewUIView addGestureRecognizer:singleFingerTap];
}
[self setupScroll];
}
I think you can use UITableView and custom UITableViewCell for this purpose. On click of "Add More", add one entry to NSMutableArray and just reload tableview.
And no need to manage index separately, you can get it directly by indexPath.row
Also you don't need to manage scroll view size as tableview automatically adjust is content height according to row.
If you are using same kind of view,then go with this approach, as its easy and will save your time.

Scrolling the map to user's location or clicking on an annotation on the map causes the container view move back to the place set in the storyboard

I have
a view controller named MyMapViewController which is the delegate of the map view that is in the storyboard,
a view controller (i.e. a container view, 'myContainerView', in the storyboard) named MyListViewController that is the child view controller of MyMapViewController.
So, I have the following in the storyboard: http://pasteboard.co/1YAmM2ZU.png
In MyMapViewController, I set myContainerView's frame initially as follows so that the only part shown would be its navigation bar at the bottom of MyMapViewController:
CGRect destFrame = self.myContainerView.frame;
CGFloat navBarHeight = 44;
destFrame.origin.y = self.view.frame.size.height - navBarHeight;//to bottom of the screen; navigation bar yields 44 points
[UIView animateWithDuration:0
animations:^{
self.myContainerView.frame = destFrame;
}
];
In MyMapViewController, I set some annotations on the map and the delegate method is as follows:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{
NSLog(#"welcome into the map view annotation");
// if it's the user location, just return nil.
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
// try to dequeue an existing pin view first
static NSString* AnnotationIdentifier = #"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
pinView.pinColor=MKPinAnnotationColorPurple;
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"annotationProfile.png"]];
pinView.leftCalloutAccessoryView = profileIconView;
return pinView;
}
The problem is that when I scroll the map to user's own location or to the location of an annotation that is not initially shown on the map or click on one of the annotations, myContainerView shows up suddenly. (it moves back to the place set in the storyboard.) However, I want myContainerView to stay where it was.
I came up with some possible explanations why it happens but am not quite sure how I can overcome this problem. Any suggestions?
Thanks in advance.
When you face a user interface abnormality such as the one in the question, then there is surely something you miss about the design. If this is the case, check if you solve the problem by manipulating the constraints you defined in the storyboard. You can bring the constraint into the code just by pressing ctrl and dragging it in your code and thus create a property. Then you can easily change the constraint's constant value in your code, for example for moving the related view to where you want in superview.
If this would not work, try implementing another UI design approach. For instance, you could create a .xib file for the view. This way, you can easily manipulate every single move of your view inside its superview just by changing its frame inside the superview. This approach gives you a much more elasticity than the one that in storyboard, but harder to implement.

Adding subviews in NSWindow while resizing the windows, the UI gets jumbled

I have a window and a subview is added, this subview is the container of another view(accessed and added using NSViewController).
I have disabled autolayout and doing resizing from springs. The subviews gets resize correctly on window resize.
If I add / remove subviews keeping the window size same it works fine. But if I add subview and maximize it and then remove and then add, it gets jumbled.
Some time it happens straight forward as :
Open the main window (it opens in small size). Maximize it, then add the subview, the subview is added to its original size as drawn in xib. Expected behavior is the subview should get expand and cover the main window.
I am not able to find the solution. Please help me to fix this. The sample code and sample project is attached here.
//In AppDelegate
- (IBAction)buttonClicked:(id)sender {
if (!self.myVC) {
self.myVC = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
}
[self.containerView addSubview:self.myVC.view];
}
- (IBAction)clearClicked:(id)sender {
for (NSView *view in self.containerView.subviews) {
[view removeFromSuperview];
}
}
I got the answer,
Before adding the view to container view, I get the container's rect and set the frame for child view.
- (IBAction)buttonClicked:(id)sender {
if (!self.myVC) {
self.myVC = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
}
NSRect rect = NSMakeRect(0, 0, self.containerView.frame.size.width, self.containerView.frame.size.height);
[self.myVC.view setFrame:rect];
[self.containerView addSubview:self.myVC.view];
}

iOS - UITableViewCell's image go ontop of table upon selection?

I have 2 tables in one view. Table A lists a bunch of users. Table B lists a users objects. When a row is selected in Table A, Table B is reloaded with the objects that belong to that user.
So when a user selects a row in Table A, the image in the background of the cell changes to the highlighted version of the image.
Here is the normal version of the background image:
Here is the highlighted version of the background image:
As you can see, the highlighted version has a small arrow on the right of it. This arrow is beyond the width of the table cell the table itself. When the row is selected, the image changes as it should, but the image is sized down to fit the whole image into the cell.
What I would like to happen is the image goes outside of the table, or on top of the table for that selected row.
One possible solution I thought was to center the table on the selected row and then overlay that image, but if the user was to try to scroll through the table, the image would need to move and that would be a big pain.
So what I would like to know is it is possible to extend the cell's size beyond the table one it is selected?
Thanks in advance!
EDIT:
The following does not work, just in case anyone was going to try:
[cell setFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width+20, cell.frame.size.height)];
Setting a views clipsToBounds property to NO will allow the view to draw outside of its own frame.
In your UITableViewController subclass:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do what you normally do here, if anything then add...
self.view.clipsToBounds = NO;
}
Doing this has a side effect where you will see full cells be created at the bottom or top of the tableview instead of them scrolling partially into view. Either put the table view into another view that has clipsToBounds set to YES, align the edges of the table view with the edges of the screen, or have views covering over the bottom and top (like a UIToolbar and UINavigationBar normally would).
To get the UITableViewCell's selectedBackgroundView to extend past the table view's frame create a subclass of UITableViewCell and override the layoutSubviews method.
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
self.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"YQGyZ"]];
self.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"CQXYh"]];
self.textLabel.backgroundColor = [UIColor clearColor];
self.detailTextLabel.backgroundColor = [UIColor clearColor];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect frame = self.selectedBackgroundView.frame;
frame.size.width += 13; // where 13 is the size of the arrow overhang from the same images
self.selectedBackgroundView.frame = frame;
// You can also change the location of the default labels (set their background colors to clear to see the background under them)
self.textLabel.frame = CGRectMake(70, 0, 148, 30);
self.detailTextLabel.frame = CGRectMake(70, 30, 148, 30);
}
Good luck.
I would recommend modifying the image resource of the unselected cell background such that it is the same width as the selected background, but just has a transparent rectangle on the side.

UIView initWithFrame overwrite view height

I have a UIView with a NIB. The view in the NIB is set to 280 x 390.
When I add the View inside another view I "something" do this
view_name *view = [view_name alloc] initWithFrame:CGRectMake(0, 0, 280.00, 390.00) ];
[outer_view addSubview: view];
Now in certain instances I need to make the view smaller as there is other content on the page. I tried to change the initWithFrame variables but it does not work.
How can I do this?
Cheers!
put a uiscrollview, set its size to your desired one, and add your view as a subview to the scrollview.