UIView Not Changing Size On Rotation Autolayout - ios

I have a UIScrollView with a UIView for content inside of it. On the UIView, I have some buttons I'm using for a menu. Everything works great until I rotate to landscape. Once I rotate, the buttons on the UIView can't be clicked but the top buttons still work. I'm assuming it's because the UIView holding the buttons isn't being resized so it's not responding to taps. This can be seen in the 3d exploded image (Image 4). I have tried all of these lines of code to try to get it to resize, buttons have worked (code is commented because I tried different combos).
override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
//hides and displays tab bar on orientation changes
if toInterfaceOrientation.isLandscape {
tabBarController!.tabBar.hidden = true
//self.navigationController!.view.sizeToFit()
//self.navigationController!.view.setNeedsLayout()
//self.viewForContent.setNeedsLayout()
//self.viewForMenuItems.setNeedsLayout()
//self.viewForContent.contentSize = CGSizeMake(900, 900)
//self.viewForContent.setNeedsLayout()
}
else if toInterfaceOrientation.isPortrait {
tabBarController!.tabBar.hidden = false
//self.navigationController!.view.setNeedsLayout()
//self.viewForContent.setNeedsLayout()
//self.viewForMenuItems.setNeedsLayout()
//self.viewForContent.contentSize = CGSizeMake(900, 900)
//self.viewForContent.setNeedsLayout()
}
}
Also I have this in the DidLoad and DidLayoutSubviews. When I comment out the DidLayoutSubviews the buttons work again, but the scrollview isn't large enough to allow me to scroll to the bottom buttons when in landscape
override func viewDidLoad() {
super.viewDidLoad()
let size = UIScreen.mainScreen().bounds.size
viewForContent.contentSize = CGSizeMake(size.width, 458)
// Do any additional setup after loading the view.
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let size = UIScreen.mainScreen().bounds.size
viewForContent.contentSize = CGSizeMake(size.width, 458)
}
The My Information, My Staff, My Colleagues, and My Residents are the buttons that work in landscape mode.
I have used autolayout constraints to setup the views. Also, I am using XCode7 with Swift.
Any help would be appreciated
Thanks

First I setup my views as outlined in the link posted above in the comments by Kusul (Thank you). But when I did this and added the buttons to the content view they wouldn't show on when the screen was rotated to landscape because they were off of the bottom and the UIScrollView didn't "grow".
I figured out that I needed to add a bottom constraint to my last button. So the last button now has a constraint to the top and the bottom. This forced the scroll view to "grow" to the proper size. I could then eliminate setting the size in the didLoad and didLayoutSubviews. Thanks for the help

Related

Xcode setNavigationBarHidden moves up view

To explain my problem, I will show you a screenshot of what happens.
After that, I will show the storyboard and the code that I use.
There are actually two problems, which I think are related.
My app UI looks as follows:
When scrolling down, I get the following behaviour:
Navigationbar and tabbar disappear, this is desired behaviour.
Note the white bar below the red bar, this is not desired behaviour and I'm not sure where it comes from.
Edit
When making the NewsfeedPageCell blue, I get the following:
When scrolling back to the top, the result is:
Suddenly the newsfeed rendered is positioned too high.
My storyboard looks as follows:
The newsfeed rendered is a reusable View that is loaded from an xib.
Edit: the newsfeed item cell is a reusable view loaded from an xib.
As far as I know, I have added all the required anchors in the storyboard.
The code of my Newsfeed class that handles the visibility of the navigationbar and the tabbar on scroll:
func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
// Remove navigationbar and tabbar on scroll
if(velocity.y > 0) {
parentViewController?.navigationController?.setNavigationBarHidden(true, animated: true)
parentViewController?.tabBarController?.hideTabBarAnimated(hide: true)
} else {
parentViewController?.navigationController?.setNavigationBarHidden(false, animated: true)
parentViewController?.tabBarController?.hideTabBarAnimated(hide: false)
}
}
I've been stuck with this problem for a while.
Any help is appreciated, and if you need more info, please ask.
Edit: The constraints are as follows:
Constraints for the Newsfeed Pager:
Edit2: Debug view navbar hidden:
Debug view without navbar hidden:
Looks like the top anchor of the menu is to the status bar.
Try:
Change MenuBar.top to superview.top, instead of Safe Layout.tom
As a quick fix:
override viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .red /// or the MenuBar backgroundColor tiny color
}
I think y implement all things right.
if you face the problem devices ios 11.0+ then the problem is UIScrollViewContentInsetAdjustmentBehavior when navigation bar is hidden which is UIScollView behavior
you need to set
self.collectionView.contentInsetAdjustmentBehavior = .never
the default value for it is .automatic so scroll starting after the status bar.

Swift3 center UIbutton

ok, I have added a button to the .swift storyboard, I have then added the following code to my ViewController.swift
#IBOutlet weak var HelloButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
HelloButton.frame.origin.x = view.frame.size.width / 2
}
However when I test it on my iPhone 5s it moves it to the right of my screen and does not make it centered. I am unsure what I need to do to make the button centered.
Yes this is my first helloWorld app, and maybe over my head, but I understand web programing, and I understand to make it center automaticilly I need to have the screen view width total and the button width. I then need to say to the button to divide the screen view width by 2.
for example in CSS we do this
#button{margin:0 auto;}
but this is not HTML or CSS. IT'S SWIFT3
Try this:
HelloButton.center.x = view.frame.width/2
Yours is not correct because the origin.x is the first point (top left) of the button view, not the center of the view, you can change it's background color to different color to see it
Also, the variable name should be lowerCase, followed by code convention
You should be using Autolayout for accomplishing what you want and the way you do is like the following in your ViewController inside of Main.storyboard: (control + left mouse click -> dragging the blue line upwards and then releasing the control and left mouse click, you will see the popup)
In the popup select the options:
Center Horizontally In Container
Center Vertically in Container
But if you want to do it programmatically, do it in viewWillLayoutSubviews method:
class ViewController: UIViewController {
#IBOutlet weak var button: UIButton!
override func viewWillLayoutSubviews() {
super. viewWillLayoutSubviews()
button.center = view.center
}
}
In your project Autolayout is enabled? if Auto Layout enabled then Change UI in viewdidLoad: may not give appropriate result. use -viewWillLayoutSubviews method to do UI task, it you want to center your button
myButton.center = self.view.center
Hope it will work.
Add the code to viewDidAppear that way the actual view has shown up. Also, you should use button.center.x instead of button.frame.origin.x if you want to center it

Fitting a UIScrollView to a UIView

I am trying to use a UIScrollView to show a series of UIViews. In my storyboard I have a View Controller containing a UIView that is constrained using AutoLayout.
View Controller (UIView in grey)
In order to call the UIScrollView I am using the following method:
func initScrollview() {
self.mainScrollView = UIScrollView(frame: self.mainView.bounds)
self.mainScrollView!.contentSize = CGSizeMake((self.mainView.bounds.width)*CGFloat(3), self.mainView.frame.height)
self.mainScrollView!.backgroundColor = UIColor.greenColor() // For visualization of the UIScrollView
self.mainScrollView!.pagingEnabled = true
self.mainScrollView!.maximumZoomScale = 1.0
self.mainScrollView!.minimumZoomScale = 1.0
self.mainScrollView!.bounces = false
self.mainScrollView!.showsHorizontalScrollIndicator = true;
self.mainScrollView!.showsVerticalScrollIndicator = false;
self.mainScrollView!.delegate = self
for i in 0...3 {
var tempView = SubView(frame: self.mainView.bounds)
pages.insert(tempView, atIndex: i)
self.mainScrollView!.addSubview(pages[i]);
}
self.mainScrollView!.scrollRectToVisible(CGRectMake(mainScrollView!.frame.size.width, 0, mainScrollView!.frame.size.width, mainScrollView!.frame.size.height), animated: false)
}
When I run my code, the UIScrollView does not fit the UIView. Instead it is too short and too wide. The result looks like this:
UIView in grey, UIScrollView in green
What am I doing wrong that is causing the UIScrollView to be incorrectly sized?
You should put the codes that you init the UI element sizes base on the screen size(UIView of UIViewController) in viewDidLayoutSubviews. Because in viewDidLoad, the screen didn't adjust its size yet,
In the above code, there no mention of adding the mainScrollView to the mainVew.
To whose view are you adding the mainScrollView? My opinion would be you are trying to add it to self.view whereas it should be to self.mainView
After the initScrollView() function is called try adding it the below code
self.mainView.addSubview(mainScrollView!)
This would probably be easier if you had placed all these views directly in your storyboard instead of programatically. I don't see anything in your code that can't be done visually in IB. Also, if you have autoLayout active in your storyboard, setting frames and sizes in code won't work. (auto-layout will change your values on the next pass)

Changing the height of the Navigation bar iOS Swift

I am trying to change the height of there navigation bar for my app.
Currently the height is fixed to 44. I can change the width from Xcode but not the height.
I have no idea how to change this. Very new to iOS development.
Can anyone please help?
simply dragged and dropped it on my view
In that case, the simplest way is with constraints. Just give it a height constraint (along with the other constraints that position it). No code required! Here's an example:
That was achieved with no code at all. It's all done with constraints:
We are pinned to the top and sides of the superview, along with height constraint of 100.
Try this :
import UIKit
class YourViewController : UIViewController {
var navBar: UINavigationBar = UINavigationBar()
override func viewDidLoad() {
super.viewDidLoad()
self.setNavBarToTheView()
// Do any additional setup after loading the view.
self.title = "test test"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setNavBarToTheView() {
self.navBar.frame = CGRectMake(0, 0, 320, 50) // Here you can set you Width and Height for your navBar
self.navBar.backgroundColor = (UIColor.blackColor())
self.view.addSubview(navBar)
}
}
I know this makes no sense, however this is what I did ( worked without laying down constraints ).
select your View Controller.
Open Show the attributes inspector
for top bar select any tab bar (I'm choosing translucent Tab Bar).
within the show the object library drag and drop the navigation item on the View Controller. (If done right, should look like image 3)
Additionally ( fyi ), you can add a constraint to your button, etc. with using no margins and top being set to 0.

Mixing storyboard and constraints in code

I'm trying to implement view controller from this wireframe
As we have different screen resolutions - different number of yellow views can fit on the screen. All yellow views are same size UIImageViews, containing the same image.
What I'm trying to do is to layout view controller in storyboard except the yellow views. Then I calculate number of yellow views screen can fit and add them using auto layout in code. Like this:
let numberOfViewsPerSide = numberOfViewsOnEachSide()
let viewImage = UIImage (named: "repeat_image")
var prevView = centerImageView
if let viewImage = viewImage {
for index in 0...(numberOfViewsPerSide - 1) {
var newView = UIImageView(image: viewImage)
newView.setTranslatesAutoresizingMaskIntoConstraints(false)
let views = ["prevView" : prevView, "newView" : newView]
self.view.addSubview(newView)
let constraint_H = NSLayoutConstraint.constraintsWithVisualFormat("H:[newView]-15-[prevView]", options: NSLayoutFormatOptions.AlignAllCenterY, metrics: nil, views: views)
self.view.addConstraints(constraint_H)
prevView = newView
}
}
The problem is that in viewDidLoad and viewWillAppear methods auto layout isn't triggered yet. So centerImageView doesn't have right size and position. Moreover its superview is nil at this point. So I basically can't add constraint to view without parent.
My code works in viewDidAppear method but that means that all yellow views will appear only after transition animation is completed.
Do you have any idea how to implement that without doing everything in storyboard or purely in code. UILabel, UIButton on the picture positioned deleted to the green centerView - if I add centerView in code I need to set all constraints for this screen in code.
UPDATE:
Solution offered by #pteofil worked for me
so I call code above in viewWillAppear like that:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.view.layoutIfNeeded()
addRepeatedImages()
}
You can use layoutIfNeeded to get the autolayout triggered and have your views the correct size.
But your yellow views almost looks like you could use a UICollectionView to manage them...
You can embed all the yellow views in a container view you position in interface builder. Now you can do all the stuff inside this view in code.

Resources