Tableview change constraints to full screen when switch to landscape












1















Basically, I have a tableview and when I switch to landscape I want it to set the table view to fullscreen and when I switch it back to portrait, it should revert to default constraints. It works if I remove the breakpoint.



However, when I switch it back from landscape to portrait, it does not return to default. When I switch it back to portrait, I am editing the constraints for it to do what I want. However, I am open to other solutions. Please advise me on what I should do. I added the breakpoint using UIViewAlertForUnsatisfiableConstraints just like this image:



breakpoint log



override func viewDidLoad() {
super.viewDidLoad()
parseData(noOfPosts:90)

TableView.delegate = self
TableView.dataSource = self
TableView.tableFooterView = UIView()

let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)

myTop2Constraint.isActive = true
myBtm2Constraint.isActive = true

if(UIDevice.current.orientation.isLandscape){
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false
}
}

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)
let myTopConstraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0)
let myBtmConstraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)

myTopConstraint.isActive = false
myBtmConstraint.isActive = false
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false

if(UIDevice.current.orientation.isLandscape){
self.catIsuSemasa.isHidden = true
self.catOthers.isHidden = true
self.catSocial.isHidden = true
self.catPolitics.isHidden = true
if(myTop2Constraint.isActive){
print("error1?")
myTop2Constraint.isActive = false
print("error2?")
myBtm2Constraint.isActive = false
print("error3?")
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}else{
TableView.removeAllConstraints()
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}
}else{
self.catIsuSemasa.isHidden = false
self.catOthers.isHidden = false
self.catSocial.isHidden = false
self.catPolitics.isHidden = false
if(myTopConstraint.isActive){
print("error")
myTopConstraint.isActive = false
print("error2")
myBtmConstraint.isActive = false
print("error3")
myTop2Constraint.isActive = true
print("error4")
myBtm2Constraint.isActive = true
print("run?")
}
}
}

extension UIView {
func removeAllConstraints() {
self.removeConstraints(self.constraints)
for view in self.subviews {
view.removeAllConstraints()
}
}
}









share|improve this question

























  • why you are set constrain programmatically you can do this same by storyboard using size class (vary for traits)?. this link might be help you to set different constrain for landscape and portrait in storyboard.

    – Jatin Kathrotiya
    Nov 22 '18 at 4:15













  • I am looking into it now, thanks for the pointer

    – Zack Cheang Weng Seong
    Nov 22 '18 at 4:24











  • @JatinKathrotiya hey, thanks for pointing me in the right direction, I manage to use the vary for traits solution to fix my problem. If you post it it the answer's comment, ill make it a solution

    – Zack Cheang Weng Seong
    Nov 22 '18 at 5:18











  • Most Welcome , I added as answer so it will help full to other user

    – Jatin Kathrotiya
    Nov 22 '18 at 5:33
















1















Basically, I have a tableview and when I switch to landscape I want it to set the table view to fullscreen and when I switch it back to portrait, it should revert to default constraints. It works if I remove the breakpoint.



However, when I switch it back from landscape to portrait, it does not return to default. When I switch it back to portrait, I am editing the constraints for it to do what I want. However, I am open to other solutions. Please advise me on what I should do. I added the breakpoint using UIViewAlertForUnsatisfiableConstraints just like this image:



breakpoint log



override func viewDidLoad() {
super.viewDidLoad()
parseData(noOfPosts:90)

TableView.delegate = self
TableView.dataSource = self
TableView.tableFooterView = UIView()

let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)

myTop2Constraint.isActive = true
myBtm2Constraint.isActive = true

if(UIDevice.current.orientation.isLandscape){
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false
}
}

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)
let myTopConstraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0)
let myBtmConstraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)

myTopConstraint.isActive = false
myBtmConstraint.isActive = false
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false

if(UIDevice.current.orientation.isLandscape){
self.catIsuSemasa.isHidden = true
self.catOthers.isHidden = true
self.catSocial.isHidden = true
self.catPolitics.isHidden = true
if(myTop2Constraint.isActive){
print("error1?")
myTop2Constraint.isActive = false
print("error2?")
myBtm2Constraint.isActive = false
print("error3?")
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}else{
TableView.removeAllConstraints()
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}
}else{
self.catIsuSemasa.isHidden = false
self.catOthers.isHidden = false
self.catSocial.isHidden = false
self.catPolitics.isHidden = false
if(myTopConstraint.isActive){
print("error")
myTopConstraint.isActive = false
print("error2")
myBtmConstraint.isActive = false
print("error3")
myTop2Constraint.isActive = true
print("error4")
myBtm2Constraint.isActive = true
print("run?")
}
}
}

extension UIView {
func removeAllConstraints() {
self.removeConstraints(self.constraints)
for view in self.subviews {
view.removeAllConstraints()
}
}
}









share|improve this question

























  • why you are set constrain programmatically you can do this same by storyboard using size class (vary for traits)?. this link might be help you to set different constrain for landscape and portrait in storyboard.

    – Jatin Kathrotiya
    Nov 22 '18 at 4:15













  • I am looking into it now, thanks for the pointer

    – Zack Cheang Weng Seong
    Nov 22 '18 at 4:24











  • @JatinKathrotiya hey, thanks for pointing me in the right direction, I manage to use the vary for traits solution to fix my problem. If you post it it the answer's comment, ill make it a solution

    – Zack Cheang Weng Seong
    Nov 22 '18 at 5:18











  • Most Welcome , I added as answer so it will help full to other user

    – Jatin Kathrotiya
    Nov 22 '18 at 5:33














1












1








1








Basically, I have a tableview and when I switch to landscape I want it to set the table view to fullscreen and when I switch it back to portrait, it should revert to default constraints. It works if I remove the breakpoint.



However, when I switch it back from landscape to portrait, it does not return to default. When I switch it back to portrait, I am editing the constraints for it to do what I want. However, I am open to other solutions. Please advise me on what I should do. I added the breakpoint using UIViewAlertForUnsatisfiableConstraints just like this image:



breakpoint log



override func viewDidLoad() {
super.viewDidLoad()
parseData(noOfPosts:90)

TableView.delegate = self
TableView.dataSource = self
TableView.tableFooterView = UIView()

let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)

myTop2Constraint.isActive = true
myBtm2Constraint.isActive = true

if(UIDevice.current.orientation.isLandscape){
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false
}
}

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)
let myTopConstraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0)
let myBtmConstraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)

myTopConstraint.isActive = false
myBtmConstraint.isActive = false
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false

if(UIDevice.current.orientation.isLandscape){
self.catIsuSemasa.isHidden = true
self.catOthers.isHidden = true
self.catSocial.isHidden = true
self.catPolitics.isHidden = true
if(myTop2Constraint.isActive){
print("error1?")
myTop2Constraint.isActive = false
print("error2?")
myBtm2Constraint.isActive = false
print("error3?")
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}else{
TableView.removeAllConstraints()
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}
}else{
self.catIsuSemasa.isHidden = false
self.catOthers.isHidden = false
self.catSocial.isHidden = false
self.catPolitics.isHidden = false
if(myTopConstraint.isActive){
print("error")
myTopConstraint.isActive = false
print("error2")
myBtmConstraint.isActive = false
print("error3")
myTop2Constraint.isActive = true
print("error4")
myBtm2Constraint.isActive = true
print("run?")
}
}
}

extension UIView {
func removeAllConstraints() {
self.removeConstraints(self.constraints)
for view in self.subviews {
view.removeAllConstraints()
}
}
}









share|improve this question
















Basically, I have a tableview and when I switch to landscape I want it to set the table view to fullscreen and when I switch it back to portrait, it should revert to default constraints. It works if I remove the breakpoint.



However, when I switch it back from landscape to portrait, it does not return to default. When I switch it back to portrait, I am editing the constraints for it to do what I want. However, I am open to other solutions. Please advise me on what I should do. I added the breakpoint using UIViewAlertForUnsatisfiableConstraints just like this image:



breakpoint log



override func viewDidLoad() {
super.viewDidLoad()
parseData(noOfPosts:90)

TableView.delegate = self
TableView.dataSource = self
TableView.tableFooterView = UIView()

let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)

myTop2Constraint.isActive = true
myBtm2Constraint.isActive = true

if(UIDevice.current.orientation.isLandscape){
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false
}
}

override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
let myTop2Constraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 50)
let myBtm2Constraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 50)
let myTopConstraint:NSLayoutConstraint = TableView.topAnchor.constraint(equalTo: view.topAnchor, constant: 0)
let myBtmConstraint:NSLayoutConstraint = TableView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0)

myTopConstraint.isActive = false
myBtmConstraint.isActive = false
myTop2Constraint.isActive = false
myBtm2Constraint.isActive = false

if(UIDevice.current.orientation.isLandscape){
self.catIsuSemasa.isHidden = true
self.catOthers.isHidden = true
self.catSocial.isHidden = true
self.catPolitics.isHidden = true
if(myTop2Constraint.isActive){
print("error1?")
myTop2Constraint.isActive = false
print("error2?")
myBtm2Constraint.isActive = false
print("error3?")
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}else{
TableView.removeAllConstraints()
myTopConstraint.isActive = true
myBtmConstraint.isActive = true
}
}else{
self.catIsuSemasa.isHidden = false
self.catOthers.isHidden = false
self.catSocial.isHidden = false
self.catPolitics.isHidden = false
if(myTopConstraint.isActive){
print("error")
myTopConstraint.isActive = false
print("error2")
myBtmConstraint.isActive = false
print("error3")
myTop2Constraint.isActive = true
print("error4")
myBtm2Constraint.isActive = true
print("run?")
}
}
}

extension UIView {
func removeAllConstraints() {
self.removeConstraints(self.constraints)
for view in self.subviews {
view.removeAllConstraints()
}
}
}






ios swift uitableview view constraints






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 5:17









kit

1,1063816




1,1063816










asked Nov 22 '18 at 4:02









Zack Cheang Weng SeongZack Cheang Weng Seong

459




459













  • why you are set constrain programmatically you can do this same by storyboard using size class (vary for traits)?. this link might be help you to set different constrain for landscape and portrait in storyboard.

    – Jatin Kathrotiya
    Nov 22 '18 at 4:15













  • I am looking into it now, thanks for the pointer

    – Zack Cheang Weng Seong
    Nov 22 '18 at 4:24











  • @JatinKathrotiya hey, thanks for pointing me in the right direction, I manage to use the vary for traits solution to fix my problem. If you post it it the answer's comment, ill make it a solution

    – Zack Cheang Weng Seong
    Nov 22 '18 at 5:18











  • Most Welcome , I added as answer so it will help full to other user

    – Jatin Kathrotiya
    Nov 22 '18 at 5:33



















  • why you are set constrain programmatically you can do this same by storyboard using size class (vary for traits)?. this link might be help you to set different constrain for landscape and portrait in storyboard.

    – Jatin Kathrotiya
    Nov 22 '18 at 4:15













  • I am looking into it now, thanks for the pointer

    – Zack Cheang Weng Seong
    Nov 22 '18 at 4:24











  • @JatinKathrotiya hey, thanks for pointing me in the right direction, I manage to use the vary for traits solution to fix my problem. If you post it it the answer's comment, ill make it a solution

    – Zack Cheang Weng Seong
    Nov 22 '18 at 5:18











  • Most Welcome , I added as answer so it will help full to other user

    – Jatin Kathrotiya
    Nov 22 '18 at 5:33

















why you are set constrain programmatically you can do this same by storyboard using size class (vary for traits)?. this link might be help you to set different constrain for landscape and portrait in storyboard.

– Jatin Kathrotiya
Nov 22 '18 at 4:15







why you are set constrain programmatically you can do this same by storyboard using size class (vary for traits)?. this link might be help you to set different constrain for landscape and portrait in storyboard.

– Jatin Kathrotiya
Nov 22 '18 at 4:15















I am looking into it now, thanks for the pointer

– Zack Cheang Weng Seong
Nov 22 '18 at 4:24





I am looking into it now, thanks for the pointer

– Zack Cheang Weng Seong
Nov 22 '18 at 4:24













@JatinKathrotiya hey, thanks for pointing me in the right direction, I manage to use the vary for traits solution to fix my problem. If you post it it the answer's comment, ill make it a solution

– Zack Cheang Weng Seong
Nov 22 '18 at 5:18





@JatinKathrotiya hey, thanks for pointing me in the right direction, I manage to use the vary for traits solution to fix my problem. If you post it it the answer's comment, ill make it a solution

– Zack Cheang Weng Seong
Nov 22 '18 at 5:18













Most Welcome , I added as answer so it will help full to other user

– Jatin Kathrotiya
Nov 22 '18 at 5:33





Most Welcome , I added as answer so it will help full to other user

– Jatin Kathrotiya
Nov 22 '18 at 5:33












1 Answer
1






active

oldest

votes


















1














App support landscape and portrait then we do not required set constrain programatically we can achieve same thing by using storyboard's size class (vary for traits). By using size class we can create adaptive layout design. we can set different constrain for landscape and portrait app.



Please check below tutorial link :



Tutorial Link






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423700%2ftableview-change-constraints-to-full-screen-when-switch-to-landscape%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    App support landscape and portrait then we do not required set constrain programatically we can achieve same thing by using storyboard's size class (vary for traits). By using size class we can create adaptive layout design. we can set different constrain for landscape and portrait app.



    Please check below tutorial link :



    Tutorial Link






    share|improve this answer




























      1














      App support landscape and portrait then we do not required set constrain programatically we can achieve same thing by using storyboard's size class (vary for traits). By using size class we can create adaptive layout design. we can set different constrain for landscape and portrait app.



      Please check below tutorial link :



      Tutorial Link






      share|improve this answer


























        1












        1








        1







        App support landscape and portrait then we do not required set constrain programatically we can achieve same thing by using storyboard's size class (vary for traits). By using size class we can create adaptive layout design. we can set different constrain for landscape and portrait app.



        Please check below tutorial link :



        Tutorial Link






        share|improve this answer













        App support landscape and portrait then we do not required set constrain programatically we can achieve same thing by using storyboard's size class (vary for traits). By using size class we can create adaptive layout design. we can set different constrain for landscape and portrait app.



        Please check below tutorial link :



        Tutorial Link







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 5:31









        Jatin KathrotiyaJatin Kathrotiya

        419210




        419210
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423700%2ftableview-change-constraints-to-full-screen-when-switch-to-landscape%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?