简体   繁体   English

UIbutton忘记宽度和高度

[英]UIbutton forgets width and height

I set the UIbutton to a frame width and height using the following:我使用以下方法将 UIbutton 设置为框架宽度和高度:

let button = UIButton()
button.frame = CGRect(x: 0, y: 0, width: 31, height: 31) //set the frame

and that works.这有效。

在此处输入图片说明

However when I close the app and reopen I get this strange thing happening where the app can't remember the width and height of the button.但是,当我关闭应用程序并重新打开时,我会在应用程序无法记住按钮的宽度和高度的情况下发生这种奇怪的事情。

and instead does this (see image) instead of staying in the 31 by 31.而是这样做(见图)而不是停留在 31 x 31。

右键

Now how have I set this button?现在我如何设置这个按钮?

With the following code.使用以下代码。

// Image needs to be added to project.
                     let button = UIButton()
                     button.frame = CGRect(x: 0, y: 0, width: 31, height: 31) //set the frame
                     // let modifier = AnyImageModifier { return $0.withRenderingMode(.alwaysOriginal) }
                     button.kf.setImage(with: fburl, for: .normal, placeholder: nil)
                     button.addTarget(self, action: Selector(("yourActionFunc")), for: .touchUpInside)
                     let barButton = UIBarButtonItem()
                     barButton.customView = button
                     self.tabBarController?.navigationItem.rightBarButtonItem = barButton
                    //END FACEBOOK PROFILE IMAGE

I don't believe it has anything to do with the code above I thought it was something about the function:我不相信它与上面的代码有任何关系,我认为它与函数有关:

override func viewDidAppear(_ animated: Bool) {

    print(defaults.string(forKey: defaultsKeys.socialLogin))
     self.tabBarController?.navigationItem.title = "DRN1"
    let sessionsocial = self.defaults.string(forKey: defaultsKeys.socialLogin)
    if sessionsocial == nil {
     // Image needs to be added to project.
     let buttonIcon = UIImage(systemName: "person.circle")

    let rightBarButton = UIBarButtonItem(title: "Person", style: UIBarButtonItem.Style.done, target: self, action: #selector(FirstViewController.checkLogin(_:)))
     rightBarButton.image = buttonIcon

      self.tabBarController?.navigationItem.rightBarButtonItem = rightBarButton
       //self.tabBarController?.navigationItem.rightBarButtonItem.backgroundColor = UIColor(patternImage: UIImage(systemName: "ng")
    //self.tabBarController?.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: "person.circle", target: self, action: nil)

    }
  }

But I know it is not running that code, because I would see a person.circle image instead of my big head.但我知道它没有运行那个代码,因为我会看到一个 person.circle 图像而不是我的大脑袋。 How can I solve this strange issue?我该如何解决这个奇怪的问题?

在此处输入图片说明

If you are going to set the frame of the button manually you need to do it in viewDidLayoutSubviews so that it gets set correctly every time the parent view's frame changes.如果您要手动设置按钮的框架,则需要在viewDidLayoutSubviews中进行设置,以便每次父视图的框架更改时都能正确设置。 The idiomatically correct way to do it though is to use auto layout instead of manual frame sizing.惯用的正确方法是使用自动布局而不是手动调整框架大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM