i have created an UIScrollView with this hierarchy (my sample is AutoLayout)
UIViewController
UIView
UIScollView
ContainerView
UIImageView
everything work fine in iOS 8 - 9 expect iOS 7
i can zoom my ImageView in iOS 7
i have implemented :
-(void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
CGSize scrollableSize = CGSizeMake(self.imageView.frame.size.width,self.imageView.frame.size.height);
[self.scrollView setContentSize:scrollableSize];
self.scrollView.backgroundColor = [UIColor whiteColor];
self.scrollView.minimumZoomScale = 1.0 ;
self.scrollView.maximumZoomScale = 4;
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.containerView;
}
i have checked this links :
UIScrollView zooming with Auto Layout
Scroll View not functioning IOS 7
Problem
zoom not work in iOS 7 , ( i need to use Auto Layout ) and my UIImageview should have a AspectFit Ratio
i uploaded my sample on github :
sampleProject
Related
I have a UIView in .xib file which I'm loading at runtime and setting it as tableHeaderView of UITableView. I have a UILabel in my xib file which can grow dynamically with fixed UIButton at bottom.
If I set the width of UILabel to fixed width it works.
If I set the Leading/Trailing on UILabel then it doesn't work :(
I'm using the below code to handle the height of headerView
-(void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
// Dynamic sizing for the header view
if (table.tableHeaderView) {
UIView *headerView = table.tableHeaderView;
float height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect headerFrame = headerView.frame;
// If we don't have this check, viewDidLayoutSubviews() will get
// repeatedly, causing the app to hang.
if (height != headerFrame.size.height) {
headerFrame.size.height = height;
headerView.frame = headerFrame;
table.tableHeaderView = headerView;
}
}
}
Anybody can explain why setting the leading/trailing is not working?
My view with constraint is like (without fixed width):
call in the end of viewDidLayoutSubviews.
[viewWithLabelAndButton setNeedLayout];
[viewWithLabelAndButton layoutIfNeeded];
I have a wired bug in iOS7, my UIScrollView content height is out of screen bounds and I can't scroll down to the bottom.
I'm using AutoLayout.
The view heirarchy is:
- UIView
- UIScrollView
- UIView
- Buttons
In iOS8+ it's working fine.
I have this in viewDidLoad:
if ([self respondsToSelector:#selector(automaticallyAdjustsScrollViewInsets)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
And this also:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.scrollView.contentSize = CGSizeMake(320, 1380);
}
iPhone 6 simulator:
iPhone5 hardware:
Channel Header:
- (id)initWithFrame:(CGRect)frame title:(NSString *)title {
self = [super initWithFrame:frame];
UILabel *l = [[UILabel alloc] initWithFrame:frame];
l.textAlignment = NSTextAlignmentCenter;
l.backgroundColor = BGC;
l.text = title;
[self addSubview:l];
return self;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
ChannelHeader *h = [[ChannelHeader alloc] initWithFrame:CGRectMake(0, 0, self.tv.frame.size.width, 44) title:[self titles][section]];
return h;
}
Why is my tableview header's textfield showing up off center?
Tableview frame in iPhone6 sim:
<UITableView: 0x7fce2c821a00; frame = (0 0; 414 736)
EDIT:
so I changed my simulated nib size from "iPhone 6" to "inferred", and the view boundaries grew. I stretched my UITableView to fit the bounds and now the text is even more off-centered. So somehow it's getting the wrong values for its frame..
Well, your frame for iPhone 6 is wrong.
iPhone 6 screen size is 375 x 667 points.
iPhone 6 plus screen size is 414 x 736 points.
Therefore, if you are running the application on iPhone 6 simulator and tableView frame is giving you iPhone 6 plus boundaries, then that is the error. Your header and UILabel is being rendered correctly according to the given frames.
So if you run your application in iPhone 6 plus simulator, you will get correct results.
More info on frames.
Solution:
If your setting your tableView through nib, and if you are using AutoLayout then you need to apply constraints accordingly.
If you have disabled AutoLayout then apply proper resizing masks to your tableView, i.e. Flexible Height & Flexible Width
Also, it would be a good practice, if you take values of the tableView frame provided by the delegate method instead of referring to the tableView property.
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
ChannelHeader *h = [[ChannelHeader alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 44) title:[self titles][section]];
return h;
}
Note: This is just a piece of good practice and it doesn't effect the logic or the answer.
Whilst I await your response on the above comment, I'm quite certain I know why you are having an issue.
If you want to create UITableViewCell(s) which have centred labels there are really only two correct ways.
Either:
Subclass UITableViewCell and create a custom subclass which has an indigenously centred label.
Set the NSTextAlignment of the regular UITableViewCell textLabel control to centre in cellForRowAtIndexPath like so: cell.textLabel!.textAlignment = .Center.
To change the header text alignment, you should do something like this:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UILabel * sectionHeader = [[UILabel alloc] initWithFrame:CGRectZero];
sectionHeader.backgroundColor = [UIColor clearColor];
sectionHeader.textAlignment = NSTextAlignmentCenter;
sectionHeader.font = [UIFont boldSystemFontOfSize:10];
sectionHeader.textColor = [UIColor whiteColor];
return sectionHeader;
}
I am creating a custom keyboard on iOS using Objective-C. To increase the height of the keyboard to make it same as default keyboard height (with suggestions bar) for both portrait and landscape, I have added height constraints programatically { I removed the previously added constraint before adding the new one }.
I am updating the height in viewWillLayoutSubviews method of the class so that on orientation change, the desired height of the keyboard is achieved and it is working perfectly fine.
But my issue is that on orientation change, the view does not autolayout perfectly as it does in default keyboard on iOS, rather the view changes and then updates the height and then expands to the right of the screen to cover the complete area. ( basically a jerk happens before the keyboard completely loads itself )
Also, on initialization of the keyboard, the view is loaded with the default height and then it updates the custom height. Post that, it applies the auto layout constraint and updates the UI, hence providing the complete layout of the keyboard screen. The whole process looks a bit jerky.
I am using xib with auto layout.
In case I do not update the custom height of the keyboard, the above mentioned jerks are solved but the height of the keyboard remains default allowed by apple.
There is another thing that there are a lot of views in XIB and hence a lot of constraints. If I remove the constrains, the keyboard loads pretty quick and the custom height is also achieved. But as soon as I rotate the device, the view does not layouts and I am stuck with half keyboard on the screen.
Any suggestion would be great!
- (id)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];
if (self) {
// Perform custom initialization work here
if (IS_IPAD) {
self.portraitHeight = 300;
self.landscapeHeight = 300;
}else{
self.portraitHeight = 257;
self.landscapeHeight = 203;
}
}
return self;
}
- (void)updateViewConstraints {
[super updateViewConstraints];
// Add custom view sizing constraints here
if (_viewWillAppearExecuted)
[self adjustHeight];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:YES];
[self.view addConstraint:self.heightConstraint];
_viewWillAppearExecuted = YES;
}
#pragma mark - Setters/Getters
- (NSLayoutConstraint *)heightConstraint
{
if (!_heightConstraint) {
_heightConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0.0 constant:self.portraitHeight];
_heightConstraint.priority = UILayoutPriorityRequired - 1;
}
return _heightConstraint;
}
#pragma mark - Methods
- (void)adjustHeight
{
if (self.view.frame.size.width == 0 || self.view.frame.size.height == 0)
return;
[self.view removeConstraint:self.heightConstraint];
// [self.imageView removeConstraint:self.heightConstraint];
CGSize screenSize = [[UIScreen mainScreen] bounds].size;
CGFloat screenH = screenSize.height;
CGFloat screenW = screenSize.width;
BOOL isLandscape = !(self.view.frame.size.width ==
(screenW*(screenW<screenH))+(screenH*(screenW>screenH)));
self.isLandscape = isLandscape;
if (isLandscape) {
self.heightConstraint.constant = self.landscapeHeight;
// [self.imageView addConstraint:self.heightConstraint];
[self.view addConstraint:self.heightConstraint];
} else {
self.heightConstraint.constant = self.portraitHeight;
// [self.imageView addConstraint:self.heightConstraint];
[self.view addConstraint:self.heightConstraint];
}
}
Try this codes for your problem. First of all do some R&D with this code and let me know.
I have a really big problem with UIScrollView. To make sure it wasn't my actual project, I created a NEW project, iPhone only, for iOS 6 and 7. I disabled autolayout.
I created a Tab Bar project
I created a view controller,
embedded it in a navigation controller and connected it as Tab bar
item #0
I put a UIScrollView in my view.
I connected the scrollview in the custom UIViewController created for this view.
I synthesized my IBOutlet and set the delegate to self. I also
implemented delegate in .h
Here is my code (ViewDidLoad):
self.scrollView.delegate = self;
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)];
NSLog(#"contentSize width %f", self.scrollView.contentSize.width);
NSLog(#"contentSize height %f", self.scrollView.contentSize.height);
NSLog(#"%#", NSStringFromCGAffineTransform(self.scrollView.transform));
It returns me "contentSize width 20000.000000", "contentSize height 0.000000
" and "[1, 0, 0, 1, 0, 0]". It scrolls left-right where it should scroll up-down
Please help!
I had the same issue until I realized I hadn't set the UIScrollView delegate. Also, if you're using auto layout, you need to wait for the layout to be applied to the UIScrollView. This means you should set the Content Size in "viewDidLayoutSubviews".
In viewDidLoad method, frame may not be correct, move the code to this method:
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
// Your code
}
Use this code. ScrollView setContentSize should be called async in main thread.
Swift:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
DispatchQueue.main.async {
var contentRect = CGRect.zero
for view in self.scrollView.subviews {
contentRect = contentRect.union(view.frame)
}
self.scrollView.contentSize = contentRect.size
}
}
Objective C:
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
dispatch_async(dispatch_get_main_queue(), ^ {
CGRect contentRect = CGRectZero;
for(UIView *view in scrollView.subviews)
contentRect = CGRectUnion(contentRect,view.frame);
scrollView.contentSize = contentRect.size;
});
}
Okay, there is absolutely nothing wrong with your code. So some suggestions:
Make sure you added to your .h file like so:
#interface yourViewController : UIViewController <UIScrollViewDelegate> {
}
Try moving this snippet of code to the "viewWillLoad" method:
- (void)viewWillAppear:(BOOL)animated {
self.scrollView.delegate = self;
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:CGSizeMake(self.view.frame.size.width, 20000)];
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
var contentRect = CGRect.zero
for view: UIView in scrollView.subviews {
if !view.isHidden {
contentRect = contentRect.union(view.frame)
}
}
scrollView.contentSize = contentRect.size
scrollView.contentSize.width = self.view.frame.width
}
Working Swift 3.X code. Converted #Karen Hovhannisyan's answer.