UIScrollview as main view IOS 8 - ios

i'm trying to make a uiscrollview as main view in IOS 8 but i can't.
I have the following structure:
View
Header_View (Like TabBar)
UIScrollView
ContentView
UIImage
UILabel_Text
UIButton
Sometimes the text is bigger than others so i don't know the exactly height of the view. With this structure when i try to scroll vertical nothing happens.
Could someone help me?
Thanks.

First take a int variable like ajustHeight in ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : ViewController{
int ajustHeight;
}
then in ViewController.m use this variable to get UILabel_Text height like below.
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
UILabel *successlabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 215, 75)];
successlabel.text = message;
successlabel.numberOfLines = 0;
successlabel.textAlignment = NSTextAlignmentLeft;
[successlabel sizeToFit];
[expandableview addSubview:successlabel];
ajustHeight = successlabel.frame.size.height;
}
Now you get successlabel height after that you can adjust the view and scrollview content height as well. if you create UILable in for loop then user like ajustHeight += successlabel.frame.size.height;

Related

Change height of view programmatically in uistackview

I need to change the view height in the stack view when I press the test button, but it is not working properly.
When I press the test button, I want to set the height of view 3 to 50 and the height of view5 to fill the remaining area. When I press the test button again, i want to reverse to process. How can I do that?
Thank you.
As #SeanLintern88 mentioned, the way you really should be doing this is with auto layout constraints -- you don't want to be mixing setFrame with autolayout.
IBOutlet the height constraints for View 3 and View 5. Set the View 3 height constraint as inactive to start (if you want it to look like your storyboard does currently to start), then whenever the button is pressed, check which constraint is active and flip-flop them.
#import "ViewController.h"
#interface ViewController ()
#property (strong, nullable) IBOutlet NSLayoutConstraint *view3HeightConstraint;
#property (strong, nullable) IBOutlet NSLayoutConstraint *view5HeightConstraint;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// start us out as inactive
self.view3HeightConstraint.active = NO;
}
- (IBAction)btnPressed:(id)sender {
if (self.view5HeightConstraint.active) {
// view 5 height constraint is active
// you can set the height constants directly in storyboard as well
self.view3HeightConstraint.constant = 50.0f;
self.view3HeightConstraint.active = YES;
self.view5HeightConstraint.active = NO;
} else {
// view 3 is height constraint is active
// you can set the height constants directly in storyboard as well
self.view5HeightConstraint.constant = 50.0f;
self.view5HeightConstraint.active = YES;
self.view3HeightConstraint.active = NO;
}
// animate the layoutIfNeeded so we can get a smooth animation transition
[UIView animateWithDuration:1.0f animations:^{
[self.view layoutIfNeeded];
}];
}
#end

Tableheader view autolayout issue

I'm loading a custom UIView's XIB file as a header view of a uitableview inside a view controller.
The file owner for the xib file is the viewcontroller. I have both the viewcontroller's & the uiview's interface declared inside the uiviewcontroller.
ViewController.h
#class ZeroStateView;
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, weak) IBOutlet CustomUITableView *tableView;
#property (nonatomic,strong) NSMutableArray *dataArray;
#property (weak, nonatomic) IBOutlet ZeroStateView *zeroStateView;
#end
#interface ZeroStateView : UIView
#property (weak, nonatomic) IBOutlet AutoLayoutLabel *titleLabel;
#property (weak, nonatomic) IBOutlet UIImageView *titleIcon;
- (void)updateView;
#end
ViewController.m
- (void)prepareHeaderViewForZeroState{
ZeroStateView *sizingView = [ZeroStateView new];
[[[NSBundle mainBundle] loadNibNamed:#"ZeroStateView" owner:self options:nil] objectAtIndex:0];
sizingView = self.zeroStateView;
[sizingView updateView];
self.tableView.tableHeaderView = sizingView;
UIView *headerView = self.tableView.tableHeaderView;
CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect headerFrame = headerView.frame;
headerFrame.size.height = height;
headerView.frame = headerFrame;
self.tableView.tableHeaderView = headerView;
}
#end
#implementation ZeroStateView
-(void)updateView{
self.titleIcon.alpha = 0.5;
UIFontDescriptor *titleFontDescriptor = [UIFontDescriptor preferredFontDescriptorWithTextStyle:UIFontTextStyleSubheadline];
self.titleLabel.text = #"This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. This is a long text message and its really long. ";
}
The AutolayoutLabel class the following method overridden:
- (void)setBounds:(CGRect)bounds {
[super setBounds:bounds];
// For multiline label, preferredMaxLayoutWidth always matches the frame width
if (self.numberOfLines == 0 && bounds.size.width != self.preferredMaxLayoutWidth) {
self.preferredMaxLayoutWidth = self.bounds.size.width;
[self setNeedsUpdateConstraints];
}
}
The height calculated by the systemLayoutSizeFittingSize:UILayoutFittingCompressedSize returns 0. As a result I get the following view as the table header view:
When I added the actual height as below, the uilabel overflows. I'm expecting the uiview to grow as the label height grows.
headerFrame.size.height = self.sizingView.frame.size.height;
Here is the screen capture of that UIViews constraints:
What do I miss here? Can someone point me out?
Update
I created a sample project for you guys to check on whats exactly issue is.
I reimplemented what you had so far in a different way. To start, I removed the UIView in ZeroStateView.xib that the UIImageView and UILabel were embedded in. The base of the xib is already a UIView, so it is unnecessary to add another UIView to it.
Next I changed the constraints around. I don't remember exactly what constraints I changed, so I will just list them out here:
Constraints for UIImageView
Align Center X to Superview
Width = 60
Height = 56
Top Space to Superview = 37
Bottom Space to UILabel = 31
Constraints for UILabel
Left Space to Superview = 15
Right Space to Superview = 15
Bottom Space to Superview = 45
Top Space to UIImageView = 31
Onto the code. In ViewController.h, the IBOutlet wasn't doing anything as far as I could tell, so I changed that property to read #property (strong, nonatomic) ZeroStateView *zeroStateView;
Now the important changes: ViewController.m. There are two UITableViewDelegate methods that will replace prepareHeaderViewForZeroState. In viewDidLoad, initialize the zeroStateView and set the table view's delegate to self.
- (void)viewDidLoad
{
//...
// Load the view
self.zeroStateView = [[[NSBundle mainBundle] loadNibNamed:#"ZeroStateView" owner:self options:nil] firstObject];
[self.zeroStateView updateView];
self.zeroStateView.backgroundColor = [UIColor darkGrayColor];
// Set self for table view delegate for -heightForHeaderInSection: and viewForHeaderInSection:
self.dataTable.delegate = self;
}
Now that we are the table view's delegate, we get two method calls that will allow us to customize the header view and set its height appropriately.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
// This will set the header view to the zero state view we made in viewDidLoad
return self.zeroStateView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
// sizeToFit describes the size that the label can fit itself into.
// So we are saying that the label can use the width of the view and infinite height.
CGSize sizeToFit = CGSizeMake(self.view.frame.size.width, MAXFLOAT);
// Then we ask the label to tell us how it can fit in that size.
// The label will respond with the width of the view and however much height it needs
// to display its text. This is the magic of how it grows vertically.
CGSize size = [self.zeroStateView.titleLabel sizeThatFits:sizeToFit];
// Add the height the label needs to the overall zero state view. This should be changed
// to the height of the UIImage + the height we just got + the whitespace above and below
// each of these views. You can handle that part.
return self.zeroStateView.frame.size.height + size.height;
}
I uploaded my changes to Dropbox here.
Just changing the size of the frame of the tableHeaderView, doesn't change it's size. You have to set it again to the tableView to force a reload.
You have to call this again self.tableView.tableHeaderView = headerView; after setting the new frame size.
As you call prepareHeaderViewForZeroState method from viewwillappear. At that point you layout is not calculate. so force layout to calculate before calling systemLayoutSizeFittingSize method to calculate height of cell. Here are the code that you need to write before calling systemLayoutSizeFittingSize.
UIView *header = self.tableView.tableHeaderView;
[header setNeedsLayout];
[header layoutIfNeeded];
Edit :
You just left 1 constraints in ZeroStateView.xib. that is Bottom Space to : Superview. kindly refer screenshot.
Output :
Here you have Updated code
Hope this help you.
I'm not sure but you could check if you have a leading,trailing,top and bottom constraints for both the UIImage and the label with reference to the superview.
Edit:
Add the width constraint before getting the systemLayoutSize
NSLayoutConstraint *tempWidthConstraint =
[NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:CGRectGetWidth(window.frame)];
widthConstraint.constant = tempWidthConstraint.constant;
[self.contentView addConstraint:tempWidthConstraint];
CGSize fittingSize = [self.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
CGFloat height = fittingSize.height +1;
[self.contentView removeConstraint:tempWidthConstraint];
There are a few things here that are confusing to me that might be complicating things.
Why are you subclassing the label?
The constraints don't really make sense.
Why does the label have two height constraints, a constant and a ≥ constraint? Remove them both, you don't need either.
Also, your vertical space to the bottom of the container is ≥ as well. Why? As it stands, your custom view doesn't have a defined height, which could be why the fitting size returns a height of zero.
Once you resolve those issues, let me know if you have better luck.

Update the preferredMaxLayoutWidth for a multiline UILabel with unknown size (Auto Layout)

I have a custom view class which inherits from UIView. This class has an UILabel as its subview. In the init-function of this custom view class I set up everything needed like this:
//h-file
#import <UIKit/UIKit.h>
#interface MyCustomView : UIView
#property (strong, nonatomic) UILabel *myLabel;
#end
//m-file
#implementation MyCustomView
#synthesize myLabel = _myLabel;
- (id)init
{
self = [super init];
if (self) {
_myLabel = [UILabel new];
if(_textView){
_myLabel.highlightedTextColor = [UIColor whiteColor];
_myLabel.translatesAutoresizingMaskIntoConstraints = NO;
_myLabel.lineBreakMode = NSLineBreakByWordWrapping;
_myLabel.numberOfLines = 0;
_myLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_myLabel];
}
}
return self;
}
#end
I also set up a bunch of constraints to manage padding inside my custom view - furthermore there are constraints to layout multiple MyCustomView-instances for both vertical and horizontal axis as well.
To get a multilined label output I have to set the preferredMaxLayoutWidth-property of the UILabel myLabel. The width depends on the free space available. At http://www.objc.io/issue-3/advanced-auto-layout-toolbox.html I read, that I can let Auto Layout calculate the width first and set it as preferredMaxLayoutWidth after the frame of the MyCustomView-instance (the label inside is single lined at this moment) has been set.
If I put the following function into the MyCustomView, the label still has a single line of text:
- (void)layoutSubviews
{
[super layoutSubviews];
float width = _myLabel.frame.size.width;
_myLabel.preferredMaxLayoutWidth = width;
[super layoutSubviews];
}
If I set the preferredMaxLayoutWidth to an explicit value inside the init-function, the label is multilined.
Does anybody know what I am doing wrong here?
Thanks in advance!
Without seeing all the constrains you have setup for your custom view, and the superview that contains it, it's really hard to determine the problem, I suggest you to print out all the view frames of the entire view hierarchy starting from the view controller's view at viewDidLayoutSubviews and determine if the label and its superviews have correct frame set.
I have an encountered similar issues with dynamic label size and scroll view so I created a prototype here, might be useful to you too: https://github.com/briandotnet/AutoLayoutScrollViewExperiment

UIImageView not correctly updating bounds

I am currently trying to program a game with a HP gauge represented by an UIImageView placed on a .xib file. I declared my IBOutlet as follows:
#property (weak, nonatomic) IBOutlet UIImageView *hpGaugeView;
and synthesized accordingly. It is also linked to the UIImageView instance in the xib.
In the xib, I set the length of hpGaugeView to 188 and height to 9.
Now this is what I did in viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[hpGaugeView setFrame:CGRectMake(49, 18, 123, 9)];
NSLog(#"%f, %f", [hpGaugeView frame].size.width, [hpGaugeView frame].size.height);
}
Although NSLog tells me the hpGaugeView's width is 123, when I run it the length appears unchanged compared to the one in xib. All the other setFrame: or setBound: worked fine in other viewControllers, so I'm just wondering what's happening here.
It is a small but crucial problem that is bugging me for a few hours straight...
Edit:
When I turn off AutoLayout, the resizing worked; it creates a bunch of other problems though...
With autolayout on, the subviews have not been laid during the viewDidLoad. The constraints are calculated a bit later and you can override the viewDidLayoutSubviews method to set your frame:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
// Do any additional setup after subviews are laid.
[hpGaugeView setFrame:CGRectMake(49, 18, 123, 9)];
NSLog(#"%f, %f", [hpGaugeView frame].size.width, [hpGaugeView frame].size.height);
}
From the class reference:
viewDidLayoutSubviews
Notifies the view controller that its view just laid out its subviews.
I believe the problem is that you're not changing the frame of the UIImageView. The bounds refer to the position and size of the view in it's own coordinate system whereas the frame refers to the position and size in it's superview coordinate system.
If the clipsToBounds property is set yo YES, then nothing outside the frame of your view will be visible.
It turns out it is some weird weird interaction between the nib and the code...when I created the UIImageView programmatically it worked fine.
AutoLayout property worked only above IOS 6.0 only . Dragging of control to XIB instead of you can create the UIImageView through Code and add to current view. I have given the code for adding UIImageView programmatically.
Source Code:
// .h file
#property (weak, nonatomic) UIImageView *hpGaugeView;
//.m file
- (void)viewDidLoad
{
[super viewDidLoad];
hpGaugeView = [[UIImageView alloc] init];
[hpGaugeView setFrame:CGRectMake(49, 18, 123, 9)];
[self.view addSubview:hpGaugeView];
}
Initialize and add the hpGaugeView to subview inside the viewDidLoad function through programmatically.

UIButton Changing Position

I have a button set up in IB. I have an IBOutlet set up and the onscreen object linked to it. Is there a way to programmatically change that buttons position and/or size? I know you can change the title and some things but I don't see how to change it's position or size.
Now I would like to change the position of it accordingly. Is it possible? If yes, please let me know how, since I am trying to change the position of my button in the following code, but it does not work in the header file.
#property (nonatomic, strong) IBOutlet UIButton *mybuttonOutlet;
In the implementation file:
-(void)viewDidLoad {
screenSizeHeight=[UIScreen mainScreen].bounds.size.height;
if(screenSizeHeight==568)
mybuttonOutlet.frame= CGRect(38, 464 ,157,25);
if(screenSizeHeight==480)
mybuttonOutlet.frame= CGRect(38, 364 ,157,25);
}
Remove Use Autolayout from the button in IB or storyboard.
If you want to adjust positions with Autolayout enabled, you will have to change your code like this
-(void)viewDidLayoutSubviews {
screenSizeHeight=[UIScreen mainScreen].bounds.size.height;
if(screenSizeHeight==568)
mybuttonOutlet.frame= CGRect(38, 464 ,157,25);
if(screenSizeHeight==480)
mybuttonOutlet.frame= CGRect(38, 364 ,157,25);
}
Basically you need to perform any custom layout adjustments in viewDidLayoutSubviews method if Autolayout is enabled
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
#import "ViewController.h"
#interface ViewController ()
#property(nonatomic, strong) IBOutlet UIButton *theButton;
// -(IBAction)moveTheButton:(id)sender;
#end
#implementation ViewController
#synthesize theButton;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)moveTheButton:(id)sender{
//set default position
CGRect btFrame = theButton.frame;
btFrame.origin.x = 145;
btFrame.origin.y = 285;
theButton.frame = btFrame;
//position changing
btFrame.origin.x += 40;
theButton.frame = btFrame;
//new size of button
CGRect rect=CGRectMake(145, 285, 190, 30);
[self.theButton setBounds:rect];
}
#end
I eventually went for the constraints. Get an outlet of the constraints that determine the position of the button (top, bottom, leading, trailing) and change the constraint(s) value(s).
self.constBtnSubmitTrailing.constraint = 50.0
This so you can keep AutoLayout enabled.
The solution I figured out for this is to create new View object inside the main View, and put my "dynamically changing objects" inside that View (as shown in picture).
Object hierarchy
Then I use Auto-Layout to position the new View in the main View.
But bugButton:UIButton which is inside the new View, changes position as expected with:
bugButton.frame.origin = CGPoint(x: newX, y: newY)
Please perform this check.
-(void)viewDidLoad{
if(self.mybuttonOutlet==nil)NSLog(#"Button is NIL");
}
Now if you get the log "Button is NIL", then probably you forgot to link your IB button to your variable mybuttonOutlet.

Resources