Offsetting UIRefreshControl

前端 未结 9 1450
醉话见心
醉话见心 2020-12-14 01:14

I am currently using a JASidePanel for my application and I have a UITableViewcontroller with a UIRefreshControl as one of the ViewControllers for it. The width of my tablev

相关标签:
9条回答
  • 2020-12-14 01:34

    For Swift 2.2 solution is

    let offset = -50
    
    let refreshControl = UIRefreshControl()
    refreshControl.bounds = CGRect(x: refreshControl.bounds.origin.x, y: offset,
                                   width: refreshControl.bounds.size.width,
                                   height: refreshControl.bounds.size.height)
    refreshControl.attributedTitle = NSAttributedString(string: "Pull to refresh")
    refreshControl.addTarget(self, action: #selector(networking), forControlEvents: UIControlEvents.ValueChanged)
    self.profileTable.addSubview(refreshControl)
    

    Thats it.

    0 讨论(0)
  • 2020-12-14 01:35

    You need to subclass UIScrollView, following code will adjust UIRefreshControl location based on contentInset (works only for vertical scrolling but easily adoptable for horizontal)

    override public func layoutSubviews() {
        super.layoutSubviews()
        guard let refreshControl = self.refreshControl else { return }
    
        var newFrame = refreshControl.frame
    
        if contentInset.top > .zero {
            let yOffset = abs(contentInset.top + contentOffset.y)
    
            newFrame.origin.y = -contentInset.top - yOffset
            newFrame.size.height = yOffset
        }
    
        guard newFrame != refreshControl.frame else { return }
        refreshControl.frame = newFrame
    }
    
    0 讨论(0)
  • 2020-12-14 01:45
    - (UIRefreshControl *)refreshControl
    {
        if (!_refreshControl)
        {
            _refreshControl = [UIRefreshControl new];
            _refreshControl.tintColor = [UIColor lightGrayColor];
    

    _refreshControl.bounds = CGRectInset(_refreshControl.bounds, 0.0, 10.0); // let it down with inset 10.0

            [_refreshControl addTarget:self
                                action:@selector(pullToRefresh)
                      forControlEvents:UIControlEventValueChanged];
        }
        return _refreshControl;
    }
    
    0 讨论(0)
  • 2020-12-14 01:50

    Here's a Swift version similar to the workaround that Werewolf suggested. I'm using my own custom activity view class (MyCustomActivityIndicatorView) and it's also a subview of the refresh control, so I make sure I don't mess with it's frame, just the frame of the default subviews. After calling layoutSubviews on super I adjust the custom activity view's frame to match. This is all contained in a custom UIRefreshControl subclass.

    override func layoutSubviews() {
    
        for view in subviews {
            if view is MyCustomActivityIndicatorView {
                continue
            } else {
                // UIRefreshControl sizes itself based on the size of it's subviews. Since you can't remove the default refresh indicator, we modify it's frame to match our activity indicator before calling layoutSubviews on super
                var subFrame = view.frame
                subFrame.size = activityView.intrinsicContentSize()
    
                // add some margins
                subFrame.offsetInPlace(dx: -layoutMargins.left, dy: -layoutMargins.top)
                subFrame.insetInPlace(dx: -(layoutMargins.left+layoutMargins.right), dy: -(layoutMargins.top+layoutMargins.bottom))
    
                view.frame = subFrame.integral
            }
        }
    
        super.layoutSubviews()
    
        activityView.frame = bounds
    }
    

    Note: I'm adding in UIView's layout margins in which isn't strictly necessary but gives my activity indicator some space to breath.

    0 讨论(0)
  • 2020-12-14 01:57

    Swift 5:

    There are a couple of ways to change UIRefreshControl position. The setup of the refresh control is done with the most convenient way.

    Example where the refresh control is part of the scroll view:

    let refresher = UIRefreshControl()
    refresher.addTarget... // Add a proper target.
    scrollView.refreshControl = refresher
    

    Please note that you can use tableView instead of the scrollView.

    Option 1: You can play with the frame/bounds of the view.

    scrollView.refreshControl?.refreshControl.bounds.origin.x = 50
    

    Option 2: Or you can do it using Auto Layout.

    
    scrollView.refreshControl?.translatesAutoresizingMaskIntoConstraints = false
    
    scrollView.refreshControl?.topAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
    scrollView.refreshControl?.centerXAnchor.constraint(equalTo: view.centerXAnchor, constant: -50).isActive = true
    
    0 讨论(0)
  • I needed to do this in order to move the UIRefreshControl downwards. My solution was to subclass UIRefreshControl, and overwrite the layoutSubviews method to set a CAAffineTransform translation on each subview. Unfortunately you can't just set a transform on the UIRefreshControl.

    Change xOffset and yOffset as necessary.

    @interface MyUIRefreshControl : UIRefreshControl
    @end
    
    @implementation MyUIRefreshControl
    
    - (void)layoutSubviews {
        [super layoutSubviews];
        for (UIView *view in self.subviews) {
            view.transform = CGAffineTransformMakeTranslation(xOffset, yOffset);
        }
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题