I need to bring Scroll indicator to the left side of a table view.
self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 0, self.tableView.bounds.size.width-8);
Above code is working fine for iPhone 5, 5C etc.
But scroll indicator is not positioned well in iPhone 6. It shows some padding from left side.
In viewDidLoad method, the bounds of the view are not set correctly and if view is set in nib/storyboard, bounds are set accroding to size of view set there.
I guess, in your nib.storyboard you have chosen iPhone 5S/iPhone 5S size to design the view and henceit works in those devices.
If you set tableView's scrollIndicatorInsets in viewDidLayoutSubviews method, the indicator inset should set correctly.
Related
All,
I have a scrollable InputAccessoryView attached to my UITextView.
This was created as follows:
create a UIScrollView
add a horizontal UIStackView.
Attach top, bottom, trailing, leading of stack view to scroll view.
Set equal heights between stack and scroll.
Insert lots of buttons into stack view.
set TextView.inputaccessoryview = scrollview.
Voila! (Note - only the last line was programmatic).
So this works fine and correctly scrolls when the content (stack view) is wider than keyboard, BUT when you rotate to landscape (or run on an iPad), it is left aligned.
I'd like the buttons to be centred when the keyboard is wider than the set of buttons.
I've tried embedding the Scrollview into a UIView with Center X, but that doesn't seem to work.
Can anyone give me some pointers?
Thx
Found it!
The answer is to use ContentInset on the scrollbar (which is the InputAccessoryView above). The specific code is:
public void CentreToolbar()
{
var offsetX = Math.Max((scrollView.Bounds.Width - scrollView.ContentSize.Width) / 2, 0);
scrollView.ContentInset = new UIEdgeInsets(0, (nfloat)offsetX, 0, 0);
}
This code should be called anytime the layout of the screen changes - such as during rotation.
I'm creating an UIViewController with a subview that has the same width as the super view and half the height of the super view.
Here is how it looks in the storyboard:
These are the constraints I added vor the View Punkte:
Plus I added the "Equal Height" constraint.
The constraints work correctly. The subview stays the same when I rotate the device.
But the problem are the frame sizes.
When I print the sizes of both views, that's what Xcode returns (The code is inside ViewDidLoad:
print(self.view.bounds) // returns --> (0.0, 0.0, 375.0, 667.0)
print(viewPunkte.bounds) // returns --> (0.0, 0.0, 600.0, 300.0)
Why is this happening? The width of the viewPunkte is not correct!
What is the solution to this problem?
Thanks.
If you are inspecting the frame of a view during viewDidLoad then you are probably doing so after the views have been initialized but before they have ever completed a layout pass. Your controller's view has not yet been added to the window and you should not assume that the frames of any of your views reflect what their final sizes will be.
Consider why you think you need to inspect view frames at this point in time.
Usually, the size of view controller's view in storyboard is not equal to simulator.It's square in storyboard but iOS devices are not in square shape.
It seems that you run your project in an iPhone/iPhone simulator
therefore:
self.view.bounds = iPhone screen size(pt) = (375.0, 667.0)
and you set up the height and width constraint of your 'viewPunkte', which are equal to the constant 300 & 600.
thats why the size of viewPunkte is constantly equal to (600.0, 300.0)
You can use
-(void) viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
//NSLog dimensions.
}
This should give you the correct dimensions since this event means its view has just laid out its subviews.
This is fired after viewdidload and before viewDidAppear. Orientation change can also fire viewDidLayoutSubview.
My problem is very similar to UIToolbar not displaying on iPhone 4 and iPhone 4s
I am using Xcode 7 and am trying to get a 'legacy' UINavigationController based iPhone app up and running on various iPhone screen sizes. By legacy , I mean it does not use Storyboards etc. The views are loaded from an .xib.
The app is a classic UINavigationController app with UITableViewControllers but with a UIToolBar at the bottom underneath the table view. The TableView and ToolBar are subviews of the view of the ViewController.
Works great on iPhone5/5s/6/6s.
But on the iPhone 4/4s the toolbar is off the screen. Oddly if I rotate the screen to landscape, the toolbar appears. Rotate back, it vanishes. I know this seems like prehistoric iOS code, but I am completely at a loss here and have wasted hours fiddling in Xcode and IB. I know I am missing something obvious.
It seems like the height of your table view is set to be a fixed height where the toolbar is visible beneath the table view on iPhone 5 and up. To get the toolbar to display on iPhone 4/4s requires reducing the height of the table view's frame using something like the following code.
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if (onIphone4 && portraitOrientation) {
CGFloat height = 480 - self.toolbar.frame.size.height;
self.tableView.frame = CGRectMake(0, 0, 320, height); // for iPhone 4/4s
} else if (onIphone5 && portraitOrientation) {
CGFloat height = 568 - self.toolbar.frame.size.height;
self.tableView.frame = CGRectMake(0, 0, 320, height); // for iPhone 5 and up
}
}
You will probably need to change the y position in the frame origin to account for the status bar and navigation bar if they are also present. In that case, the overall frame height will need to be adjusted for those changes. This is the frame-based way of adjusting view sizes.
The alternative and preferred method is to add auto layout constraints in the XIB for the toolbar and the table view in Interface Builder so that they will maintain the proper size and position relative to the screen dimensions.
I found the issue.. it was the "Full Screen at Launch" checkbox in IB, for the main "UIWindow"... and I quote from Apple documentation....
"If you choose to create a window in Interface Builder, be sure to select the Full Screen at Launch option in the Attributes inspector so that the window is sized appropriately for the current device."
Now please excuse me while I slam head against wall..way to waste a Sunday!
I'm writing an app in Objective-C using Xcode 6 and iOS 8. The app needs to be able to be deployed on an iPhone 5, 6, or 6+.
If you want to get straight to answering my question, jump down to the last sentence. If you want to understand why I have the question I do, or maybe how I can alter my UI layout in order to solve my problem another way, read on.
In one of my view controllers, I have a scroll view whose top is constrained to the bottom of the navigation bar, and whose bottom is constrained to the top of a table view. The table view's bottom is constrained to the bottom of the view controller's main view (i.e. to the bottom of the phone).
The scroll view contains subviews that expand/contract when the user taps on them. I want the scroll view to grow as its subviews grow, but obviously I don't want the scroll view to grow off screen because it looks bad and because it would cause unsatisfiable constraints (the table view's top--which is constrained to the bottom of the scroll view--would cross below its bottom--which is constrained to the bottom of the main view...this causes an error). So, I use the following code to make the scroll view resize itself according to its subviews sizes without growing right off the screen:
// The max height before the scroll view would go off screen, which would
// mess up the table view's constraints and cause all sorts of problems
CGFloat maxHeight = self.view.size.height
- self.navigationController.navigationBar.frame.size.height
- [UIApplication sharedApplication].statusBarFrame.size.height;
// The height of all the subviews in the scroll view.
CGFloat height = _scrollContentView.frame.size.height;
if (height > maxHeight) {
height = maxHeight;
}
self.scrollViewHeightConstraint.constant = height;
Now for the fun part. Originally, I called this code to re-evaluate and reset the size of the scroll view whenever I rotated the device from portrait to landscape, or vice versa. However, when I would rotate the phone from portrait to landscape, I was getting constraints errors. I determined that it was because I was calling this code after the rotation, when the main view's height was smaller, but the scroll view's height was still large (causing the table view's top to go below the bottom, etc. as I explained before). So, I just moved the code to be called before the rotation (I called the code in the viewWillTransitionWithSize:withTransitionCoordinator: method). This all makes sense so far.
However, now, the problem is that the navigation bar's height changes when the rotation occurs, but the viewWillTransitionWithSize:... method does not include any details on this change (it only gives the new size that the main view will be when rotation is completed, not the new size the navigation bar will be as well).
So, I need someway to determine the new size of the navigation bar before the device's orientation actually changes (just like I can determine the main view's new size before the device's orientation actually changes using the viewWillTransitionWithSize:... method).
Any ideas? TIA!
So, here's my work around in its simplest form:
/*
* This method gets called when the device is about to rotate.
*/
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
// Set the scroll view's height to 0 to avoid constraints errors as described
// in the question.
self.scrollViewHeightConstraint.constant = 0;
}
/*
* At the point when this method gets called, the device rotation has finished altering
* the frames of the views in this view controller, but the layout has not finished
* so nothing has changed on screen.
*/
- (void)viewWillLayoutSubviews
{
// The max height before the scroll view would go off screen, which would mess up
// the table view's constraints and cause all sorts of problems
CGFloat maxHeight = self.view.size.height
- self.navigationController.navigationBar.frame.size.height
- [UIApplication sharedApplication].statusBarFrame.size.height;
// The height of all the subviews in the scroll view.
CGFloat height = _scrollContentView.frame.size.height;
if (height > maxHeight) {
height = maxHeight;
}
// Reset the scroll view's height to the appropriate height.
self.scrollViewHeightConstraint.constant = height;
[super viewWillLayoutSubviews];
}
I am developing an iPhone application. I have my code implemented this way:
RootViewController has UIScrollView as its view. Then RootViewController pushes another UIViewController say vc which again has UIScrollView (sv) as its view.
The bounds/frame size of sv is (320, 460) and content size is (320, 520). So, sv is now scrollable vertically. Hence, I have set content offset of sv to be (0, 60).
I switch to background using Home button of iPhone and again put my application to Foreground. In this case my sv content offset is set to (0, 0) automatically with animation.
When I tried to override setContentOffset: I noticed that some library call [adjustsIfNeeded], sets content offset to be (0, 0).
Why is this happening?
This was because, my base view of UIViewController was UIScrollView. I changed it to a UIView and then added UIScrollView as its subview. Now, it works fine.