Resize UITableViews programmatically - ios

I have 3 tables, but sometimes I have to hide the 3 one. So:
- (void)viewWillAppear:(BOOL)animated
{
if (s == 2)
{
// Adjust frames
self.tableA.frame = CGRectMake(0, 100, 500, 200);
self.tableB.frame = CGRectMake(500, 100, 500, 200);
[self.tableC setHidden:NO];
self.tableC.frame = CGRectMake(0, 200 + 100 +40, 1024, 200);
}
else
{
self.tableA.frame = CGRectMake(0, 100, 500, 600);
self.tableB.frame = CGRectMake(500, 100, 500,600);
[self.tableA sizeToFit];
[self.tableB sizeToFit];
[self.tableC setHidden:YES];
}
The problem is that when s!=2, the tableA and tableB do not resize to take the full height I set. The have 200 as height ...
I created the tables with XIB.
I would like to resize tableA and tableB in such way they have 600 as height, but only when tableC is hidden.

Allowing Autolayout in the IB and deleting sizeToFit worked out.

Related

Creating UILabel programmatically with dynamic size

I am trying to create UILabel programmatically, but height and width should be set dynamically depending on the content. I don't want to create initial CGRect with some width and height, which cause design issues in my case.
What I tried to do is:
self.freeLabel = [[UILabel alloc] initWithFrame:CGRectMake(frameView.layer.frame.size.width - 50, -8, 120, 25)];
self.freeLabel.numberOfLines = 0;
[self.freeLabel setBackgroundColor:[UIColor colorWithRed:0.91 green:0.18 blue:0.42 alpha:1.0]];
self.freeLabel.layer.cornerRadius = 5;
self.freeLabel.layer.masksToBounds = YES;
[self addSubview:self.freeLabel];
[self sizeToFit];
but this way I cannot add the UILabel to my view.
you have to add below codes so that self.freeLabel with take new height.
[self.freeLabel sizeToFit];
self.freeLabel.frame = CGRectMake(frameView.layer.frame.size.width - 50, -8, 120, self.freeLabel.frame.size.height)];
self.frame = // update size based on the height of the label.
But I have some points which I feel are wrong.
Why x position of self.freeLabel is defined as frameView.layer.frame.size.width - 50 but width of label as 120. For sure this label will go out of your view. So frameView.layer.frame.size.width - 50 should be frameView.layer.frame.size.width - 120

UIScrollView keep buttons in fixed position

I am using UIScrollView for pinch zooming the image the code I am using is like this
CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame]; //Full Screen
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:fullScreenRect];
scroll.backgroundColor = [UIColor clearColor];
scroll.delegate = self;
scroll.contentSize = _imageView.frame.size;
[scroll addSubview:_imageView];
scroll.minimumZoomScale = 1.0;
scroll.maximumZoomScale = 5.0;
[scroll setZoomScale:scroll.minimumZoomScale];
self.view = scroll;
[scroll addSubview:_screenShotButton];
[scroll addSubview:_resolutionButton];
-(void)aligementTheControlBasedOnOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
bool ipad = (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad);
if (ipad) {
if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
_imageView.frame = CGRectMake(142, 114, 740, 555);
if ([self.resoultion isEqualToString:#"Default"]) {
_imageView.frame = CGRectMake(142, 114, 740, 555);
}
else if([self.resoultion isEqualToString:#"4:3"])
_imageView.frame = CGRectMake(142, 114, 740, 555);
else if([self.resoultion isEqualToString:#"16:9"])
_imageView.frame = CGRectMake(142, 114, 740, 416);
self.screenShotButton.frame = CGRectMake(794, 667, 55, 55);
self.resolutionButton.frame = CGRectMake(857, 667, 55, 55);
}
else
{
_imageView.frame = CGRectMake(64, 238, 640, 480);
if ([self.resoultion isEqualToString:#"Default"]) {
_imageView.frame = CGRectMake(64, 238, 640, 480);
}
else if([self.resoultion isEqualToString:#"4:3"])
_imageView.frame = CGRectMake(64, 238, 640, 480);
else if([self.resoultion isEqualToString:#"16:9"])
_imageView.frame = CGRectMake(64, 238, 640, 360);
self.screenShotButton.frame = CGRectMake(578, 749, 55, 55);
self.resolutionButton.frame = CGRectMake(649, 749, 55, 55);
}
}
}
Based on the resolution user has chosen I am positioning the buttons.
Everything is working fine for me. The problem I am facing is _screenShotButton and _resolutionButton are also moving when I zoom the image. Instead I want to keep the buttons static in the bottom right side of the screen.
Can anyone help me.
You can do it making moving sub view from UIScrollView to super view of scrollview like:
Place/set your button over scroll view (not inside scroll view) as shown here in this snapshot. And also set button constraints (position) with respect to super view of your scrollview.
Here is ref. snapshot of hierarchy of position of each view over each-other.
Do not add your buttons to the scroll view, but to its parent instead.
This part is wrong:
[scroll addSubview:_screenShotButton];
[scroll addSubview:_resolutionButton];
Assuming scroll has a superview (which in your original question it does not), do this instead:
[scroll.superview addSubview:_screenShotButton];
[scroll.superview addSubview:_resolutionButton];
Alternatively, you can pick the order of scroll vs. screenShotButton by replacing -addSubView: by one of the -insertSubview:: methods.

iOS: adding images to a UIButton, then putting button in a scrollview, but only seeing one image?

I'm working on an iPad app and part of the UI is a scrollview that has buttons for its contents. I'm working on adding images to these buttons, but when I do so, I only ever get the image in one spot, when I should be seeing it on all the buttons. This is what I'm doing:
float scrollCurrTop = 0;
CGRect currProcedureButtonFrame = CGRectMake(0,
scrollCurrTop,
self.fProceduresView.frame.size.width,
self.fLabelAndButtonHeight);
PatientIDButton* currProcedureButton = [PatientIDButton buttonWithType:UIButtonTypeCustom];
[currProcedureButton setFrame:currProcedureButtonFrame];
[currProcedureButton.layer setBorderColor: [self.fPanelViewBorderColor CGColor]];
[currProcedureButton.layer setBorderWidth: self.fBorderWidth];
currProcedureButton.titleLabel.font = self.fLabelFont;
NSString* displayName = [grabbing name];
if (displayName == nil)
{
displayName = currPlanName;
}
[currProcedureButton setTitle:displayName
forState:UIControlStateNormal];
[currProcedureButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
currProcedureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
currProcedureButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// is the plan approved?
if ([self isPlanApproved:currPlanName])
{
// add the checkmark to this plan button
CGRect currPlanButtonFrame = currProcedureButton.frame;
float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
float originY = currPlanButtonFrame.origin.y;
float width = currPlanButtonFrame.size.width - originX;
float height = currPlanButtonFrame.size.height;
CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];
[currPlanApprovalImage setBackgroundColor:[UIColor colorWithRed:(63.0f/255.0f)
green:(179.0f/255.0f)
blue:(79.0f/255.0f)
alpha:1.0f]];
[currPlanApprovalImage setImage:self.fCheckMarkIcon];
[currProcedureButton addSubview:currPlanApprovalImage];
}
[self.fProceduresView addSubview:currProcedureButton];
scrollCurrTop += self.fLabelAndButtonHeight;
Where 'fProceduresView' is the scrollview that houses the buttons. What am I doing wrong?
It seems that you're misunderstanding the logic behing setting the frame of the imageview's those you're trying to add
CGRect currPlanButtonFrame = currProcedureButton.frame;
float originX = currPlanButtonFrame.size.width - (currPlanButtonFrame.size.width/3.0f);
float originY = currPlanButtonFrame.origin.y;
float width = currPlanButtonFrame.size.width - originX;
float height = currPlanButtonFrame.size.height;
CGRect currPlanApprovalImageFrame = CGRectMake(originX, originY, width, height);
UIImageView* currPlanApprovalImage = [[UIImageView alloc] initWithFrame:currPlanApprovalImageFrame];
You don't need to set originY to currPlanButtonFrame.origin.y;
All subviews have relative coordinates to their's superviews.
So in your case originY should be 0
For example:
// Image is visible
Button frame [0, 0, 100, 100]
image in button [0, 0, 100, 100]
// Button is visible, but image is not because it will be clipped by bounds of the button.
Button frame [100, 100, 100, 100]
image in button [100, 100, 100, 100]
Also you will be able to "see" your images, if you set
currProcedureButton.clipsToBounds = NO

UIScrollview doesn't scroll iOS

I'm creating several scroll views in one view that scroll horizontally. The following code works fine:
-(void)updateSection {
builder = [NSMutableArray array];
float xPosition = 0;
float xposbut = 100;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, self.frame.size.width, self.frame.size.height - 69)];
scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
scrollView.delegate = self;
[self addSubview:scrollView];
for (int i = 1; i < itemArticleArray.count; i++) {
ItemView *item = [ItemView loadNibs];
item.frame = CGRectMake(xposbut, 10, item.frame.size.width, item.frame.size.height);
xPosition += scrollView.frame.size.width + 2;
xposbut += 500;
UIView *seperatorView = [[UIView alloc] initWithFrame:CGRectMake(xposbut - 50, 4, 2, scrollView.frame.size.height - 8)];
[scrollView addSubview:seperatorView];
xPosition += scrollView.frame.size.width + 4;
[scrollView addSubview: item];
[builder addObject:item];
}
[scrollView setContentSize:CGSizeMake(xposbut, scrollView.frame.size.height)];
[self addSubview:scrollView];
}
However, when I change the 8th line of code to the following:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 400, self.frame.size.width, self.frame.size.height - 69)];
It cause the layout to look fine, but the scroll views do then not scroll at all. Any ideas?
you set the contentSize.height to the scrollView.frame.size.height and so the scrollView cant scroll. you have to set the contentSize to the total height of you scrollView, including the not visible area. The frame is only the area on screen.
UIScrollview will scroll when content size is bigger then frame size. In your code you set content size equal to frame size that's why your scrollview is not scrolling.
Set the content size for the scroll view. The content size should be greater than frame.size, then only the scroll view will scroll. And also enable scrolling.
Use the code
scrollView.scrollEnabled=YES;
scrollView.contentSize=CGSizeMake(CGFloat width, CGFloat height);
Height should be greater than scrollView.frame.size.height and add contents inside scrollview beyond the scrolview's frame.
You must give the scroll some space to work so check this out:
Create the scroll in the storyboard
Create an outlet for the scroll
And the in the .m file you must paste these lines
The code:
(void)viewDidLoad {
[super viewDidLoad];
yourScroll.scrollEnabled=YES;
yourScroll.contentSize=CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*2);
}
/*
* yourScroll.contentSize=CGSizeMake(your desired scroll width, your desired scroll height);
* The values must be bigger than the viewController size
*/

Animating a UISlider

I'm animating a UIView, UIText, UILabel and UIImage element in my app to transition into position on button click which works perfectly. However the code I am using doesn't work on a UISlider and I can't figure out way. Can anyone help me out. My code is:
[UIView animateWithDuration:0.8f delay:0.0f options:UIViewAnimationOptionTransitionNone animations: ^{
if (!viewExpanded) { //x y width height
viewExpanded = YES; //going down
_blueMove.frame = CGRectMake(0, 0, 500, 150); //slide down panel
_titleLabel.frame = CGRectMake(30, 30, 247, 44); // player title
_timeElapsed.frame = CGRectMake(40, 95, 34, 21); //time counter
_duration.frame = CGRectMake(230, 95, 35, 21); //duration
_playerBg.frame = CGRectMake(0, 84, 320, 45); //player bg colour box
}else{ //going up
viewExpanded = NO;
_blueMove.frame = CGRectMake(0, 0, 500, 0);//slide down panel
_titleLabel.frame = CGRectMake(30, -40, 274, 44);// player title
_timeElapsed.frame = CGRectMake(40, -20, 34, 21); //time counter
_duration.frame = CGRectMake(230, -20, 35, 21); //duration
//_currentTimeSlider = CGRectMake(40, 0, 175, 34); //time slider
_playerBg.frame = CGRectMake(0, -45, 320, 45); //player bg colour box
}
You can see I've commented out an example of what I'm using in the else statement to animate my UISlider but this throws up an error.
Thanks for any help.
The code you have posted has the slider commented out
//_currentTimeSlider = CGRectMake(40, 0, 175, 34); //time slider
And it's also missing .frame

Resources