当创建带有框架的惰性按钮时,会在左上角创建它,为什么会发生这种情况?

时间:2018-10-05 02:28:55

标签: ios swift button uibutton

我创建了这个“ filterButton”,并将其设置为在“菜单按钮”的确切位置上创建,“ filterButton”直接移到视图的左上角。

lazy var filterButton: UIButton! = {
    let button = UIButton(frame: CGRect(x: self.menuButton.frame.origin.x, y: self.button.frame.origin.y, width: 45, height: 45))
    button.translatesAutoresizingMaskIntoConstraints = false
    button.backgroundColor = .blue
    button.clipsToBounds = true
    return button
}()

当我检查并打印'filterButton'的框架时,它的值为'menuButton',为什么会这样?

3 个答案:

答案 0 :(得分:0)

也许,当您访问filterButton实例时,menuButtonbutton的帧不正确。 您可以在filterButton中使用布局约束或更新viewDidLayoutSubviews的框架。

答案 1 :(得分:0)

在将filterButton添加到视图的位置添加以下约束

      myApp.controller('displayCatController', ['$scope','$http', function($scope, $http){

              //Display auditoriums information
              $http({
                  method: 'GET',
                  url:'https://developers.onemap.sg/publicapi/themeapi/retrieveTheme?queryName=auditoriums&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjMsInVzZXJfaWQiOjMsImVtYWlsIjoicHVibGljQXBpUm9sZUBzbGEuZ292LnNnIiwiZm9yZXZlciI6ZmFsc2UsImlzcyI6Imh0dHA6XC9cL29tMi5kZmUub25lbWFwLnNnXC9hcGlcL3YyXC91c2VyXC9zZXNzaW9uIiwiaWF0IjoxNTM4Mzc5ODU3LCJleHAiOjE1Mzg4MTE4NTcsIm5iZiI6MTUzODM3OTg1NywianRpIjoiMmUxZDI2NzM0YjkxYzg2N2Y4NDdkZjI1ZTVhMzQyMjgifQ.HUmGwWO7E1MJpFADcuQyVA0h6bR2Vkkp0BQrTfrEj0k'
                  }).then(function successCallback(response) {
                    $scope.auditoriums = response.data.SrchResults;
                  }, function errorCallback(response) {
                    console.log(response);
                  });


              //Display exhibitions information
              $http({
                        method: 'GET',
                  url: 'https://developers.onemap.sg/publicapi/themeapi/retrieveTheme?queryName=exhibitioncentres&token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjMsInVzZXJfaWQiOjMsImVtYWlsIjoicHVibGljQXBpUm9sZUBzbGEuZ292LnNnIiwiZm9yZXZlciI6ZmFsc2UsImlzcyI6Imh0dHA6XC9cL29tMi5kZmUub25lbWFwLnNnXC9hcGlcL3YyXC91c2VyXC9zZXNzaW9uIiwiaWF0IjoxNTM4Mzc5ODU3LCJleHAiOjE1Mzg4MTE4NTcsIm5iZiI6MTUzODM3OTg1NywianRpIjoiMmUxZDI2NzM0YjkxYzg2N2Y4NDdkZjI1ZTVhMzQyMjgifQ.HUmGwWO7E1MJpFADcuQyVA0h6bR2Vkkp0BQrTfrEj0k'
                        }).then(function successCallback(response) {
                            $scope.exhibitions = response.data.SrchResults;
                    $scope.exhibitions.splice(0,1);
                $scope.combine = $scope.exhibitions.concat($scope.auditoriums, $scope.hotels);
                console.log($scope.combine);
                        }, function errorCallback(response) {
                            console.log(response);
                        });



      }]);

答案 2 :(得分:0)

问题不是惰性按钮,请检查您menuButton的问题是,您可以检查此问题:-

lazy var filterButton: UIButton! = {
        let button = UIButton(frame: CGRect(x: 200, y: 200, width: 45, height: 45))
        button.translatesAutoresizingMaskIntoConstraints = false
        button.backgroundColor = .blue
        button.clipsToBounds = true
        return button
    }()